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';
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';
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;
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
@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.
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!
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.