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