Announcement

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

  • how to calculate age count..

    I am trying to calculate age count.. please let me know if the below command is correct..

    bys ScheduleNo: egen Age_0_14 = count(B2Q1) if Age<=14
    bys ScheduleNo: egen count_Age_0_14= max(Age_0_14)
    bys ScheduleNo: egen Age_15_24 = count(B2Q1) if Age>=15 & Age<=24
    bys ScheduleNo: egen count_Age_15_24= max(Age_15_24)
    bys ScheduleNo: egen Age_25_34 = count(B2Q1) if Age>=25 & Age<=34
    bys ScheduleNo: egen count_Age_25_34= max(Age_25_34)
    bys ScheduleNo: egen Age_35_44 = count((B2Q1)) if Age>=35 & Age<=44
    bys ScheduleNo: egen count_Age_35_44= max(Age_35_44)
    bys ScheduleNo: egen Age_45_59 = count(B2Q1) if Age>=45& Age<=59
    bys ScheduleNo: egen count_Age_45_59= max(Age_45_59)
    bys ScheduleNo: egen Age_60_120 = count(B2Q1) if Age>=60& Age<=120
    bys ScheduleNo: egen count_Age_60_120= max(Age_60_120)
    recode count_* (.= 0)

  • #2
    Half the problem here is that it is really hard to read so much code and work out whether it is "correct", not least because there is no data example here (FAQ #12). Also, what you are trying to do in any case?

    I can't imagine a good reason for all those variables.

    Consider a binning of Age:

    Code:
    gen Agebin = cond(Age <= 14, 14, cond(Age <= 24, 24, cond(Age <= 34, 34, cond(Age <= 44, 44, cond(Age <= 59, 59, cond(Age <= 120, 120, .)))))
    Hey! All of sudden I am 120.

    Now let's get the frequencies:

    Code:
    bys ScheduleNo Agebin : gen count = _N
    Now let's get a table

    Code:
    tab ScheduleNo Agebin
    Now; what is B2Q1? All that the count function does is count its non-missing values, so

    Code:
    tab ScheduleNo Agebin if !missing(B2Q1)
    should give the same result. Or slap if !missing(B2Q1) on the first statement above,

    Comment


    • #3
      okay will tray your codes Nick.. thanks a lot.. for the explanation..

      Comment


      • #4
        One ) missed off. This works for me:

        Code:
        clear
        set obs 10
        gen ScheduleNo = cond(_n <= 5, 1, 2)
        
        set seed 2803
        gen Age = runiformint(0, 119)
        
        gen Agebin = cond(Age <= 14, 14, cond(Age <= 24, 24, cond(Age <= 34, 34, cond(Age <= 44, 44, cond(Age <= 59, 59, cond(Age <= 120, 120, .))))))
        
        bys ScheduleNo Agebin : gen count = _N
        
        tab ScheduleNo Agebin

        Comment

        Working...
        X