Introductory Stata 16: Operation within Groups (bysort, _n, _N)

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • Sometimes we would like to get information over groups. For example, we may want to know the average wage for male and female workers. That is, to get the average wage for each gender group. Or you may want to figure out the minimum or maximum wage inside each group. In order to do that, we should know about Stata's underscore variables and the "bysort" prefix.
    *****************************
    16. Operation inside Groups
    *****************************
    capture log close
    log using group.log, text replace
    webuse nlsw88.dta, clear
    describe
    summarize
    tabulate occupation
    *group mean, min, and max
    bysort occupation: egen wage_group_mean=mean(wage)
    browse occupation wage wage_group_mean
    bysort occupation: egen wage_group_min=min(wage)
    bysort occupation: egen wage_group_max=max(wage)
    summarize wage wage_group_mean wage_group_min wage_group_max if occupation==2
    _n and _N
    list wage in 1/10
    display wage[1]
    display wage[10]
    display wage[_N]
    generate wage2=wage[_n-1]
    list wage wage2 in 1/10
    bysort occupation: generate id=_n
    bysort occupation: generate total=_N
    browse occupation id total
    log close
    #bysort #_n #_N

КОМЕНТАРІ •