Announcement

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

  • Generate Variable using If for multiple responses

    I am trying to generate a new variable that gives one of two "summarizing" values for four different responses for a variable (head_pt_leftside_y1):

    If the data point is 1 or 2, the new variable's value should be 0
    If the data point is 2 or 3, the new variable's value should be 1

    I am doing this to clean a data set where the collection tool software's form we used was set up rather confusingly. This is the code I tried to write to generate this variable, but I can't seem to make it work correctly. It is giving me an r(198) syntax error currently, but I think I have a larger problem with the code as a whole, as it was a "best guess" based on scattered answers for it I found online.

    PHP Code:
    gen realheadpert_leftif head_pt_leftside_y1==if head_pt_leftside_y1== 
    Thank you all very much in advance!

  • #2
    The text rules aren't consistent: if the value is 2, which is the result? 0 or 1?

    But your code suggests that you just need

    Code:
    gen realheadpert_left= inlist(head_pt_leftside, 2, 3)
    A long-winded rewriting of your syntax (showing the errors: you are misusing both | and & operators and if can only appear once in a statement) could be

    Code:
    gen realheadpert_left= 1 if (head_pt_leftside_y1==2)  | (head_pt_leftside_y1==3)
    replace realheadpert_left= 0  if (head_pt_leftside_y1==0)  | (head_pt_leftside_y1==1)
    However, if the possible values are just 0, 1, 2, 3 then the first statement suffices. There is middle ground using inlist().





    Comment


    • #3
      Oh sorry that was a typo by me.

      0 and 1 should be 0
      2 and 3 should be 1

      Thank you very much! That makes sense.
      ​​​

      Comment

      Working...
      X