Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to count the number of relevant variables present for an individual

    I have a multiple cross-sectional dataset, where I am trying to create the following index:

    Dit = (Sum all dit until k)/Kt

    In other words, sum the values for all d variables and then divide by the number d variables for that year.

    In my dataset, i have 4 variables that i need to look at but for some years only three or two are present so to calculate my index I don't want to just include individuals that had all four. I don't want them to be treated as missing. Instead, i want the aggregated variables to be divided by say 3 instead of 4 if only 3 variables are present. How do i go about creating this Kt?

    My four variables are: racmar, racpeers, racpres and racseg. They are byte variables (not string) and are non binary.


  • #2
    Thea:
    do you mean something along the following lines?
    Code:
    . set obs 3
    number of observations (_N) was 0, now 3
    
    . g id=_n
    
    . g A=runiform() in 1/2
    
    . g B=runiform() in 2/3
    
    . g C=runiform() if _n!=2
    
    . g D=runiform()
    
    . egen Overall=rowtotal(A-D)
    
    . egen Ratio=rownonmiss(A-D)
    
    . egen Denominator=rownonmiss(A-D)
    
    . g Ratio=Overall/Denominator
    
    . list
    
         +---------------------------------------------------------------------------------+
         | id          A          B          C          D    Overall   Denomi~r      Ratio |
         |---------------------------------------------------------------------------------|
      1. |  1   .3488717          .   .8689333   .0711051    1.28891          3   .4296367 |
      2. |  2   .2668857   .1366463          .    .323368      .7269          3      .2423 |
      3. |  3          .   .0285569   .3508549   .5551032   .9345149          3    .311505 |
         +---------------------------------------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment

    Working...
    X