BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anle2079
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

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

View solution in original post

3 REPLIES 3
maguiremq
SAS Super FREQ

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
ballardw
Super User

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".

Tom
Super User Tom
Super User

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. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 477 views
  • 0 likes
  • 4 in conversation