Hello, it has been a while since I've used SAS (using University Edition) and I'm not sure how to go about a problem I'm having.
I have a cohort of patients, and each patient has had data recorded at 5 visits. At each visit, it's recorded if a referral was needed (1) or not (0). So each patient might have data that looks like this:
ref1= 1
ref2=0
ref3=0
ref4=0
ref5=1
I need to create a new variable (referral) that is populated with 'yes' or 'no' if any one of the ref#=1; so if the patient ever needed a referral, during any visit, the new variable will reflect that.
Right now I am using the following code, but something isn't working. I feel like I need to use an array or DO loops, but like I say, it's been a while. Thank you for your help!
DATA want;
SET have;
IF (ref1-ref5) = . THEN referred = ' ';
ELSE IF (ref1-ref5) = 1 THEN referred = ' Yes';
ELSE IF (ref1-ref5) = 0 THEN referred = ' No';
RUN;
You can use an array or DO loop, but this is probably easier to understand and gets the job done.
data have;
input pt ref1-ref5;
datalines;
1 1 0 1 1 0
2 1 0 0 0 0
3 0 0 0 0 0
4 0 0 1 0 0
5 1 1 0 0 0
;
run;
data want;
set have;
if sum(of ref:) > 0 then referred = "Yes";
else referred = "No";
run;
Obs pt ref1 ref2 ref3 ref4 ref5 referred 1 1 1 0 1 1 0 Yes 2 2 1 0 0 0 0 Yes 3 3 0 0 0 0 0 No 4 4 0 0 1 0 0 Yes 5 5 1 1 0 0 0 Yes
You can use an array or DO loop, but this is probably easier to understand and gets the job done.
data have;
input pt ref1-ref5;
datalines;
1 1 0 1 1 0
2 1 0 0 0 0
3 0 0 0 0 0
4 0 0 1 0 0
5 1 1 0 0 0
;
run;
data want;
set have;
if sum(of ref:) > 0 then referred = "Yes";
else referred = "No";
run;
Obs pt ref1 ref2 ref3 ref4 ref5 referred 1 1 1 0 1 1 0 Yes 2 2 1 0 0 0 0 Yes 3 3 0 0 0 0 0 No 4 4 0 0 1 0 0 Yes 5 5 1 1 0 0 0 Yes
Strongly suggest that you use 1/0 coding instead of "Yes" "No".
You can get counts of "yes" by summing, percent yes by means, if all values are the same with a Range of 0 or any different with a range=1, any yes if Max is 1, a 1/0 coded variable. And a few more tricks.
The code for a 1/0 coded variable would be
Referred = max(of ref1-ref5);
If you want to Display "yes" or "no" then create a format that does so:
Proc format; value yn 1='Yes' 0='No' other=' ' ; run;
and assign the YN. format just created when you need to see "Yes".
Why would you make the new variable using a different coding scheme than the original?
referred = max(of ref1-ref5);
The MAX() over a series of binary variables test if ANY are true. The MIN() tests if ALL are true.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.