Announcement

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

  • Creata a dummy

    Hey everyone,


    I would like to create a variable which is 0 if at least one of the household members completed at least 5 years of schooling. It is supposed to turn 1 if all household members have less than 5 years of education.

    Here is the code I wrote is the following:

    Code:
    gen education = 0
    foreach var in hv108_01 hv108_02 hv108_03 hv108_04 hv108_05 hv108_06 hv108_07 hv108_08 hv108_09 hv108_10 hv108_11 hv108_12 hv108_13 hv108_14 hv108_15 hv108_16 hv108_17 hv108_18 hv108_19 hv108_20 hv108_21 hv108_22 hv108_23 hv108_24 hv108_25 hv108_26 hv108_27 hv108_28 hv108_29 hv108_30 hv108_31 hv108_32 hv108_33 hv108_34 hv108_35 hv108_36 hv108_37 {
    replace education = 1 if `var' < 5
    }
    What I now get is that the variabel is 1 if one or more members of the household are <5, but what I need is that the variable gets 1 if all members are <5.


  • #2
    Hey Lena,

    please provide a data example using dataex from ssc. That will help us in providing a solution.

    Best,
    Sebastian

    Comment


    • #3
      Use dataex to provide data examples (see FAQ Advice #12 for details). Assuming hvXXX represents the number of years of schooling:

      Code:
      clear
      input float(hv108_01 hv108_02 hv108_03 hv108_04) 
      5 4 1 7
      1 2 3 4
      8 8 8 8
      end
      
      egen wanted= rowmax(hv*)
      replace wanted = cond(missing(wanted), ., cond(wanted>=5, 1, 0))
      Res.:

      Code:
      . l
      
           +----------------------------------------------------+
           | hv108_01   hv108_02   hv108_03   hv108_04   wanted |
           |----------------------------------------------------|
        1. |        5          4          1          7        1 |
        2. |        1          2          3          4        0 |
        3. |        8          8          8          8        1 |
           +----------------------------------------------------+

      Comment


      • #4
        Code:
        egen max_yr = rowmax(hv108_*)
        generate low_edu = (max_yr < 5)
        Edit: crossed with #3

        Comment


        • #5
          Lena:
          let's consider three family members only:
          Code:
          . set obs 1
          Number of observations (_N) was 0, now 1.
          
          . g id_1=4
          
          . g id_2=3
          
          . g id_3=1
          
          . egen check=rowmax(id_*)
          
          
          . gen wanted=1 if check<5
          
          . list
          
               +-------------------------------------+
               | id_1   id_2   id_3   check   wanted |
               |-------------------------------------|
            1. |    4      3      1       4        1 |
               +-------------------------------------+
          
          .
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            thanks to everyone, this helped me a lot and it's working now! and sorry for not providing dataex, I didn't know that

            Comment

            Working...
            X