DATA Step, Macro, Functions and more

How do I combine multiple if then else statements in one operation? SAS 9.4

Reply
Occasional Contributor
Posts: 7

How do I combine multiple if then else statements in one operation? SAS 9.4

I am having a problem with a section of code; it runs fine but the frequency table that is generated is missing columns. I end up with 2 and 4 or 1 and 3 or a combination of both (I have been changing the order, etc.) New to SAS coding so hope someone can help.  I suspect I know the problem 

if icd9_code1<=0 then healthreason=1;

      else if 290<=icd9_code1<=316 then healthreason=4;

But do not know how to fix. I have tried everything I can think of! See full section of code below. Any help would be great!

 

data descriptives2;

      set descriptives;

 

*healthreason 1 is missing, 2 physical health, 3 substance use, 4 mental health;

healthreason=0;

if icd9_code1<=0 then healthreason=1;

      else if 290<=icd9_code1<=316 then healthreason=4;

if icd9_code1 in (292 303 304 305) then healthreason=3;

                  else healthreason=2;

 

 

run;

 

proc freq data=descriptives2;

      tables grantyear*member_gender*healthreason;

      title 'health reason by grant year and sex';

Super User
Posts: 19,817

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

I think you missed an ELSE.

 

if icd9_code1 <= 0 then healthreason=1;
ELSE IF 290 <= icd9_code1 <= 316 then healthreason = 4;
ELSE if icd9_code1 in (292 303 304 305) then healthreason = 3;
else healthreason = 2;
 

pammers wrote:

I am having a problem with a section of code; it runs fine but the frequency table that is generated is missing columns. I end up with 2 and 4 or 1 and 3 or a combination of both (I have been changing the order, etc.) New to SAS coding so hope someone can help.  I suspect I know the problem 

if icd9_code1<=0 then healthreason=1;

      else if 290<=icd9_code1<=316 then healthreason=4;

But do not know how to fix. I have tried everything I can think of! See full section of code below. Any help would be great!

 

data descriptives2;

      set descriptives;

 

*healthreason 1 is missing, 2 physical health, 3 substance use, 4 mental health;

healthreason=0;

if icd9_code1<=0 then healthreason=1;

      else if 290<=icd9_code1<=316 then healthreason=4;

if icd9_code1 in (292 303 304 305) then healthreason=3;

                  else healthreason=2;

 

 

run;

 

proc freq data=descriptives2;

      tables grantyear*member_gender*healthreason;

      title 'health reason by grant year and sex';


 

Occasional Contributor
Posts: 7

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

Thanks for the reply. I should have mentioned I tried it that (adding the
other else in) but it only changed the columns that showed up. It did not
give me all the columns.

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 19,817

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

Use the ELSE I provided and run a proc freq on the ICD codes by the Health Reason to see the values.

 

proc freq data=have;
table icdcode*healthreason / missing;
run;
Occasional Contributor
Posts: 7

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

Thank you. I will try that when I get back to the centre (where data is
stored).

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PROC Star
Posts: 7,474

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

You need to make a slight change to the code that @Reeza suggested because, as is, health reason 3 can never get assigned:

if icd9_code1 <= 0 then healthreason=1;
ELSE if icd9_code1 in (292 303 304 305) then healthreason = 3;
ELSE IF 290 <= icd9_code1 <= 316 then healthreason = 4;
else healthreason = 2;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 19,817

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

@art297 is correct. 

What's happening is your HealthReason codes for level 3 overlap with the generic range you're using for 4. To 'override' this you can reorder the IF statements so that 3 is assigned before 4 and then the rest that meet 4 will be assigned correctly. 

Occasional Contributor
Posts: 7

Re: How do I combine multiple if then else statements in one operation? SAS 9.4

I think I also tried that order and so 3 displayed but not 4; may be
mistaken though so will re-check. Thanks!

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Ask a Question
Discussion stats
  • 7 replies
  • 113 views
  • 0 likes
  • 3 in conversation