BookmarkSubscribeRSS Feed
Calcite | Level 5



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?

Jade | Level 19

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;
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;
101 0 . . . 1 1 2 2
102 1 0 . 2 0 . . .


Calcite | Level 5

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!



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2 in conversation