Announcement

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

  • Creating a New Variable if Another Contains a Specific Number Value

    Hello,

    I am trying to create a new variable if another numeric variable contains a specific value. From a survey, the respondent could select any option that applies. This creates a variable that contains a series of numbers (1, 123, 234, 12345, etc.). Like below:

    Var1
    1
    12
    145
    467
    2567

    I'd like to create new variables so I can count how many times each value was selected (so how many times did respondents select the first option (which will be recorded as a 1). So, in this case, the first option was selected 3 times. So, something like:

    generate option1 = 0; replace option1 = 1 if Var1 [contains the number 1]



    Thank you.

  • #2
    there is no reason to do this in 2 steps; try this:
    Code:
    gen option1=strpos(string(Var1),"1")>0
    note that I have assumed here that Var1 is numeric - if you had used -dataex- as requested in the FAQ, I would know

    also, note that you can put all of this in a loop: use forval as in:
    Code:
    forval i=1/7 {
       gen option`i'=strpos(string(Var1),"`i'")>0
    }

    Comment


    • #3
      Rich,

      Thank you very much.

      Comment

      Working...
      X