{
  "name": "Stata grouped commands and result locations",
  "description": "Original compilation of documented Stata by, egen, summarize and within-group subscripting behavior. Recipes distinguish grouping variables, within-group ordering and result location; variable names match the illustrative walkthrough rather than an empirical dataset.",
  "page": "https://casrai.org/guides/stata-bysort",
  "publisher": "CASRAI",
  "license": "https://creativecommons.org/licenses/by/4.0/",
  "dateModified": "2026-11-29T17:40:00Z",
  "temporalCoverage": null,
  "spatialCoverage": null,
  "columns": [
    {
      "name": "Task",
      "unit": "",
      "description": ""
    },
    {
      "name": "Command",
      "unit": "",
      "description": ""
    },
    {
      "name": "Group definition",
      "unit": "",
      "description": ""
    },
    {
      "name": "Within-group ordering",
      "unit": "",
      "description": ""
    },
    {
      "name": "Result location",
      "unit": "",
      "description": ""
    },
    {
      "name": "Official reference",
      "unit": "",
      "description": ""
    }
  ],
  "rows": [
    {
      "Task": "Number ordered rows",
      "Command": "bysort site (visit): generate sequence = _n",
      "Group definition": "site",
      "Within-group ordering": "Ascending visit",
      "Result location": "New variable: sequence",
      "Official reference": "by; Functions and expressions 13.7.2"
    },
    {
      "Task": "Record group row count",
      "Command": "bysort site: generate n_rows = _N",
      "Group definition": "site",
      "Within-group ordering": "Unspecified within site",
      "Result location": "New variable: n_rows",
      "Official reference": "Functions and expressions 13.7.2"
    },
    {
      "Task": "Count observed scores",
      "Command": "bysort site: egen n_scores = count(score)",
      "Group definition": "site",
      "Within-group ordering": "Unspecified within site",
      "Result location": "New variable: n_scores; nonmissing score count",
      "Official reference": "egen: count()"
    },
    {
      "Task": "Attach group mean",
      "Command": "bysort site: egen mean_score = mean(score)",
      "Group definition": "site",
      "Within-group ordering": "Unspecified within site",
      "Result location": "New variable: mean_score; site mean on each site row",
      "Official reference": "egen: mean()"
    },
    {
      "Task": "Print group summary",
      "Command": "bysort site: summarize score",
      "Group definition": "site",
      "Within-group ordering": "Unspecified within site",
      "Result location": "Printed summary per site; stored results from last group only",
      "Official reference": "summarize; by technical note"
    },
    {
      "Task": "Copy first ordered score",
      "Command": "bysort site (visit): generate first_score = score[1]",
      "Group definition": "site",
      "Within-group ordering": "Ascending visit",
      "Result location": "New variable: first_score",
      "Official reference": "Functions and expressions 13.7.2"
    }
  ],
  "sources": [
    {
      "title": "StataCorp: by",
      "url": "https://www.stata.com/manuals/dby.pdf",
      "accessed": "2026-11-17"
    },
    {
      "title": "StataCorp: egen",
      "url": "https://www.stata.com/manuals/degen.pdf",
      "accessed": "2026-11-17"
    },
    {
      "title": "StataCorp: Functions and expressions",
      "url": "https://www.stata.com/manuals/u13.pdf",
      "accessed": "2026-11-17"
    },
    {
      "title": "StataCorp: summarize",
      "url": "https://www.stata.com/manuals/rsummarize.pdf",
      "accessed": "2026-11-17"
    }
  ]
}