BookmarkSubscribeRSS Feed
pammers
Calcite | Level 5

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';

7 REPLIES 7
Reeza
Super User

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';


 

pammers
Calcite | Level 5
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. -##
Reeza
Super User

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;
pammers
Calcite | Level 5
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. -##
art297
Opal | Level 21

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

 

Reeza
Super User

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

pammers
Calcite | Level 5
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. -##

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 15850 views
  • 0 likes
  • 3 in conversation