Announcement

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

  • Problem with recoding a variable, based on two others

    Hello everyone,

    I am struggling with a basic recoding process. Basically, I have a variable called working_c19 which takes value 1 if people were working at that time, and 0 otherwise. Now, I want to recode variable so that it is only defined for those who were working at baseline period, January 2020 (variable called workin_bl).

    I tried the following, but do not provide me exactly what I want:

    Code:
    egen byte working_c19_adjusted = rowmax(working_bl working_c19)
    But basically, When I have 0 in the baseline period, and 1 in the current period, I obtain 1 in my new generated variable working_c19_adjusted.
    I want to have 1 for my new variable only when both contain the value of 1.

    Thank you in advance for your help.
    Best,

    Michael

  • #2
    MIchael:
    could you please share an example/excerpt of your dataset via -dataex-?. Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Yes of course. Sorry, my bad.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(working_bl working_c19 working_c19_adjusted)
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 0 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      0 0 0
      1 1 1
      0 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 0 1
      0 0 0
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 0 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 0 1
      0 0 0
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      1 1 1
      0 0 0
      0 0 0
      1 1 1
      1 1 1
      1 1 1
      end
      label values working_bl working_bl_val_lab
      label def working_bl_val_lab 0 "Not working (Jan-Feb.2020)", modify
      label def working_bl_val_lab 1 "Working (Jan-Feb.2020)", modify
      label values working_c19 working_c19_val_lab
      label def working_c19_val_lab 0 "Not working (Apr.2020)", modify
      label def working_c19_val_lab 1 "Working (Apr.2020)", modify
      I have put in bold some cases.
      Thank you so much for your help.

      Best,

      Michael

      Comment


      • #4
        It is the row minimum you want as then the desired result will be 1 if and only if both variables are 1. For that egen has a function rowmin(varname1 varname2) and you can also use the standard function min(varname1, varname2)

        However, watch out as the minimum of 1 and missing will be returned as missing. Depending on what you want to happen with missings, a suitable definition might be

        Code:
        gen wanted = cond(missing(varname1, varname2), ., min(varname1, varname2))
        or

        Code:
        gen wanted = cond(!missing(varname1, varname2), min(varname1, varname2), .)
        or

        Code:
        gen wanted = min(varname1, varname2) if !missing(varname1, varname2)
        or

        Code:
        gen wanted = varname1 == 1 & varname2 == 1 if !missing(varname1, varname2)

        Comment


        • #5
          Hello Nick,

          Thank you so much. I am using the last option:

          Code:
           gen wanted = varname1 == 1 & varname2 == 1 if !missing(varname1, varname2)
          And it works perfectly well.
          Best,

          Michael

          Comment

          Working...
          X