Announcement

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

  • egen variable help please

    Hello,
    I am a complete newbie to stata but I need help, would greatly appreciate any help.

    I have created a variable where a list of variables is categorised into two categories. There are 11 variables i need to categorise into a binary category. In this variable, I want the variable to be counted as 1 in any cases of 1 being selected from variable list ( e.g. var1=1 var2=2, var3= 0, var4=2 still to be counted as 1). And if all variables are scoring as 0 or 3 i wanted it to categorise into 0.

    gen newvar= .
    recode newvar .= 1 if var 1 ==1 | var 2==1| var3==1| var4==1
    recode newvar .= 0 if var 0 ==0| var 2==0| var3==0| var4==0

    but this did not account for missing data.

    Essentially i want newvar= 1 if any of the list of variables = 1, = 0 if the list of variables are equal to 0 or 2. BUT, if there are more than 30% of missing data for for the variables, for this to be categorised as missing (unless there is any instantce of 1 selected)
    so that e.g.
    var1=1 || var2=2 || var3= 0 || var4=2 || var5=0 || var6= 2 || var7= . || var8=2 || var9=2 || var10=2 || var11= 0 **this would lead to category 1 in newvar (as var1 has a value of 1)
    var1= 0|| var2=2 || var3= 0 || var4=2 || var5=0 || var6= 2 || var7= . || var8=2 || var9=2 || var10=2 || var11= 0 **this would lead to category to category 0 in newvar ( as there are no variables with 1, and only 1 variable is missing data**
    var1= . || var2=2 || var3= . || var4=. || var5=0 || var6= . || var7= 0 || var8=2 || var9=2 || var10=2 || var11= 0 **i want this to be categorised as missing, as >4 variables are missing & no variable is equal to 1).

    i created a variable of missing data using
    egen= miss= rowmiss (list of 11 variables)

    I tried adding to the recode new command if miss=3 i.e. recode newvar .= 0 if var 0 ==0| var 2==0| var3==0| var4==0 etc miss==3

    but it didn't do anything...

    Any help would be much appreciated


  • #2
    I think you want
    Code:
    unab vbles: var1 var2 var3 var4 // LIST THEM ALL HERE, BY NAME OR WITH WILDCARDS
    foreach v of varlist `vbles' {
        gen _`v' = 1.`v'
    }
    local newvars: subinstr local vbles " " " _", all
    local newvars _`newvars'
    egen wanted = rowmax(`newvars')
    egen mcount = rowmiss(`newvars')
    replace wanted = . if wanted == 0 & mcount > 0.3*`:word count `newvars''
    Note: as no example data was provided, this is not tested. Be ware of typos or other errors.

    Comment


    • #3
      Welcome to Statalist, Asmana.

      Using Statalist is just like making wild mushrooms risotto. If I asked you to make my favorite version of risotto, you'd likely need to know what the SPECIFIC ingredients are and the steps you do to make said dish.
      So far you've given neither, you're asking us for help to make something and you've not given the dataset you're dealing with or any code you've tried (or at least, the code isn't easily workable). No ingredients, no recipe=no success. So please, present your dataset using the dataex command. Once you do so, and show the code you've tried immediately after the dataex command, we'll be much more able the help you. For more details on all this, please read the FAQ section on this website.

      Comment

      Working...
      X