Search code examples
stata

New variable based on several conditions (Stata)


I have three variables structured as below:

pre_covid   change  post_covid
employed    Same         .
Housewif    Same         .
Business    diff      out of LF
employed    diff      unemployed
employed    Same         .
Housewif    Same         .

However, rather than missing (.) for those whose status has not changed since the recession started, I want to create a variable as follows post-recession_status

pre_covid   change  post_covid   post_recession_status
employed    Same         .             employed 
Housewif    Same         .             Housewif 
Business    diff      out of LF        out of LF
employed    diff      unemployed       unemployed
employed    Same         .             employed    
Housewif    Same         .             Housewif 

The dataex for the pre and post covid categories are as follows:

 1 "employed", modify
 2 "Unemployed", modify
 3 "out of LF", modify
 4 "Housewife", modify
 5 "student", modify

The dataex for the change variable is:

Example generated by -dataex-. To install: ssc install dataex
clear
input float change 
2
2
1
1
end
label values change change 
label def change 1 "Different", modify
label def change 2 "Same", modify

Solution

  • Despite edits, your data example is incomplete and inconsistent in several minor details: diff or different, Housewif or Housewife, Unemployed or unemployed, Business not a defined value label.

    Still, this shows a similar dataset as an example (which came out of editing, some extra code and then dataex) and then some token code.

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(pre_covid post_covid change)
    1 . 2
    4 . 2
    6 3 1
    1 2 1
    1 . 2
    4 . 2
    end
    label values pre_covid whatever
    label values post_covid whatever
    label def whatever 1 "employed", modify
    label def whatever 4 "housewife", modify
    label def whatever 6 "business", modify
    label def whatever 2 "unemployed", modify
    label def whatever 3 "out of LF", modify
    label values change change
    label def change 1 "different", modify
    label def change 2 "same", modify
    
    gen wanted = cond(change == 2, pre_covid, post_covid)
    label val wanted whatever
    
    list, sep(0)
    
         +-------------------------------------------------+
         | pre_covid   post_covid      change       wanted |
         |-------------------------------------------------|
      1. |  employed            .        same     employed |
      2. | housewife            .        same    housewife |
      3. |  business    out of LF   different    out of LF |
      4. |  employed   unemployed   different   unemployed |
      5. |  employed            .        same     employed |
      6. | housewife            .        same    housewife |
         +-------------------------------------------------+