Recoding with if statement

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

Jopperd
Posts: 2
Joined: Wed Apr 27, 2016 9:12 am

Recoding with if statement

Postby Jopperd » Wed Apr 27, 2016 10:07 am

Hi there,

I am having a problem with recoding my data in syntax. For some of my participants, the directions were reversed and need to be recoded (making 1 ->0 and 0->1). I would like to recode values of a variable only if another string variable has a specific string value (only if var1 = ' banana'). I thought of this:

DO IF (var1='banana').
RECODE var2 (2=1) (1=2) (ELSE=COPY) INTO var3.
END IF.
EXECUTE.

However, this also recodes all other values of var2, not just for var1 = banana.

Could anyone please tell me what I did wrong and how I could fix this?

Many thanks!
JonPedersen
Posts: 119
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Recoding with if statement

Postby JonPedersen » Sun May 01, 2016 10:09 am

Hi,
Just use do if in stead of RECODE.
i.e.

Code: Select all

DO IF (var1='banana').
   DO IF var2=2.
    compute var3=1.
  ELSE IF Var2=1.
    compute Var2=2.
  ELSE.
    compute var3=var2.
  END IF.    
END IF.
* In your particular case you can also do the somewhat more compact and opaque:.
DO IF (var1='banana').
   DO IF any(var2,1,2).
    compute var3=3-var2.
  ELSE.
    compute var3=var2.
  END IF.    
END IF.

You can also use filter.
hth
Jon
Jopperd
Posts: 2
Joined: Wed Apr 27, 2016 9:12 am

Re: Recoding with if statement

Postby Jopperd » Tue May 03, 2016 9:46 am

Thank you very much, I had not thought of using a DO IF inside a DO IF!

Kind regards,

Joppe

Who is online

Users browsing this forum: No registered users and 1 guest

cron