BookmarkSubscribeRSS Feed
Oalmend
Fluorite | Level 6

I need to combine multiple dichotomous variables into one but my code is not giving the correct output.

 

data cms_st2;
set cms_st;


If Linked=1 and then Status=1; 
If Eligible=1 then Status=2;
If Linked=0 and Eligible=0 then Status=0;

run;

The output this gives me is:

 

Status Frequency Cumulative
Frequency 0 1 2

827827
2721099
4931592

 

But Status=1 (i.e. Linked=1, regardless of whether Eligible=1 or 0) should be 765

5 REPLIES 5
Astounding
PROC Star

Your statements execute in order.  So if you assign STATUS a value of 1, it's entirely possible that the next statement changes that and assigns STATUS a value of 2.  Perhaps this is what you meant for the first two statements:

 

if Linked=1 then Status=1;

else if Eligible=1 then Status=2;

PGStats
Opal | Level 21

If Linked=1 and then Status=1;

 

is incorrect syntax.

PG
ballardw
Super User

You really want to consider using an ELSE.

If your code was actually supposed to be :

If Linked=1  then Status=1;
If Eligible=1 then Status=2;
If Linked=0 and Eligible=0 then Status=0;

 

the result for Linked=1 Eligible=1 would be Status=2 because the second if is evaluated after the first assignment.

Perhaps:

If Linked=1  then Status=1;
ELSE If Eligible=1 then Status=2;
Else if Linked=0 and Eligible=0 then Status=0;

 

 

 

 

 

Oalmend
Fluorite | Level 6

when I run

data cms_st2;
set cms_st;


If Linked=1 then Status=1; 
else If Eligible=1 then Status=2;
else If Linked=0 and Eligible=0 then Status=0;
run;

then it only assigns values for status 0 and 1, 2 is ignored.

 

ballardw
Super User

Show some of your data.

This example code exists all the possibilities of dichotomous non-missing variables coded 0 and 1 and has results of 0, 1 and 2.

data example;
input linked Eligible;

If Linked=1 then Status=1; 
else If Eligible=1 then Status=2;
else If Linked=0 and Eligible=0 then Status=0;
datalines;
0 0
1 0
0 1
1 1
;
run;

If your are not getting 1 or 2 in your data then examine your log and see if you are getting any warnings

 

or run proc freq on your data:

 

Proc freq data=cms_st;

tables linked*Eligible / list;

run;

and see what values are actually in your data.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 997 views
  • 2 likes
  • 4 in conversation