Announcement

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

  • Loops for creating categorical variables

    Hi,
    I am trying to use a loop to create new categorical variables with three groups each for 3 numerical values. I know how to do this for one variable without a loop using the gen and replace commands and then assigning labels to the values, but can someone assist me with how to accomplish this more quickly using a loop for all 3 variables? thank you!

  • #2
    Hi Adrienne,
    Actually you can do this, however, you should describe your variables in more details, and other members in the forum can help you out.

    Comment


    • #3
      I have 3 numerical variable that measure percent of arable land in three different years. For each variable, I want to create a categorical variable with three groups with percentages less than 1/3, between 1/3 and 2/3, and above 3/3 (with the labels low, moderate and high, respectively). I am not sure how to structure my foreach command...thanks.

      Comment


      • #4
        Assuming your three variables are named -var1-, -var2- and -1var3-:, you might try:

        Code:
        label define cats 0 low 1 medium 2 high
        foreach V of varlist var1 var2 var3 {
             gen `V'_cat=irecode(`V',1/3,2/3,.)
             label values `V'_cat cats
        }
        Note that -irecode- uses <= to assign values, so "low" will be <=1/3. If you need strictly < or some other boundaries, then suggest something like

        Code:
        label define cats 0 low 1 medium 2 high
        foreach V of varlist var1 var2 var3 {
             gen `V'_cat=(`V'<1/3)*0 + (`V'>=1/3)&(`V'<2/3)*1 + (`V'>=2/3)*2
             label values `V'_cat cats
        }
        hth,
        Jeph

        Comment


        • #5
          Thanks so much. The only problem I'm having is the "high category" is showing up as moderate, along with those that are actually in this group.
          This is the code I'm using:

          Code:
          label define cats 0 "low" 1 "moderate" 2 "high"
          foreach v of varlist var1 var2 var3  {
              generate `v'_cat= (`v'<=0.3)*0 + (`v'>0.3)&(`v'<=0.66)*1 + (`v'>0.66)*2
              label values `v'_cat cats
          }
          Not sure what it is about (`v'>0.66)*2 that isn't registering..

          Comment


          • #6
            It's an order of operations problem. the -generate- command should be:

            Code:
            generate `v'_cat= (`v'<=0.3)*0 + ((`v'>0.3)&(`v'<=0.66)*1) + (`v'>0.66)*2

            Comment


            • #7
              Much better- thank you Clyde and Jeph!

              Comment

              Working...
              X