Help using Base SAS procedures

Recode variables using if then across multiple variables

Reply
New Contributor
Posts: 2

Recode variables using if then across multiple variables

 

Hello, 

I am intersted in recoding or creating a new variable using logic (if/then statements) from another variable? My current code is not working:

 

Summary of data set:  I have an 21 item measure (Q1-Q21), with 25% of sessions (S1-12)

 

SecondaryObserver_S1 (can be 0 or 1) - was there a secondary observer for first sessions

PrimaryObserver_S1_Q1 (can be 0, 1, 2, or .) - Likert type variable for session 1 question 1

PrimaryObserver_S1_Q2 (can be 0, 1, 2, or .)- Likert type variable for session 1 question 2

....

PrimaryObserver_S1_Q.21 (can be 0 or 1)  -etc 

...

SecondaryObserver_S12 (can be 0 or 1)- was there a secondary observer for second session 

PrimaryObserver_S12_Q1 (can be 0, 1, 2, or .) - Likert type variable for session 2 question 1

PrimaryObserver_S12_Q2 (can be0, 1, 2, or .) - etc

....

PrimaryObserver_S12_Q.21 (0, 1, 2, or .)

 

I would like to to retain primary observer's data only if there was a secondary observer. 

 

I tried:

 

IF SecondaryObserver_S1 = 0 THEN PrimaryObserver_S1_Q1  = .

 

IF SecondaryObserver_S1 = 0 THEN PrimaryObserver_S1_Q2  = .

IF SecondaryObserver_S1 = 0 THEN PrimaryObserver_S1_Q21 = .

 

The code runs, but it is not recoding any variables. I have used if/then/else statements for recoding a variable using the logic/conditions of that variable, but never using logic of other variables. Is this possible, or are there any suggested solutions?

Trusted Advisor
Posts: 1,242

Re: Recode variables using if then across multiple variables

Hello @ecommon and welcome to the SAS Support Communities!

 

Sorry that your question hasn't been answered yet (probably due to yesterday's U.S. holiday).

 

So, you have a lot of variables, as it seems, a dataset in "wide" format.

 

Your code example could be simplified to something like

if SecondaryObserver_S1 = 0 then call missing(of PO_S1_Q[*]);

where PO_S1_Q is the name of an array (to be declared) consisting of the 21 variables on the right-hand side.

 

In any case this will set those 21 variables to missing only if the condition SecondaryObserver_S1 = 0 is met for the same observation. Since you say that it's not working, the situation in your data is probably different.

 

Can you please provide sample data in the form of a data step to clarify your current data structure and what the result should look like?

 

Here is an example which you can modify as needed. (I think it will suffice to consider only three questionnaire items and two sessions. Also, I suggest we shorten the variable names for the example.)

data have;
input id so_s1 po_s1_q1 po_s1_q2 po_s1_q3 so_s2 po_s2_q1 po_s2_q2 po_s2_q3;
cards;
101 0 2 1 0 1 1 2 2
102 1 0 . 2 0 0 1 1
;

data want;
input id so_s1 po_s1_q1 po_s1_q2 po_s1_q3 so_s2 po_s2_q1 po_s2_q2 po_s2_q3;
cards;
101 0 . . . 1 1 2 2
102 1 0 . 2 0 . . .
;

 

New Contributor
Posts: 2

Re: Recode variables using if then across multiple variables

Posted in reply to FreelanceReinhard

That is an accurate depiction of my data structure. The array and %name feature were both successful in recoding my variables. Thank you so much!

Ask a Question
Discussion stats
  • 2 replies
  • 135 views
  • 0 likes
  • 2 in conversation