Hello,
Am sorry I do not have a sample data for this question. I hope someone can still help me. I created the following code to identify the variable agegroup within the if-then-else statement but my output isn't what I wanted. My output only have '< 15 yrs'. I wanted newborn, months and days to be in one agegroup category for "0-14 yrs" while other patient_age should identify patient age in years.
data pt_age;
set patient_x;
if PATIENT_AGE <= 120 and pt_record in ("months" "days") then agegroup = '< 15 yrs';
if pt_record = "newborn" then agegroup = '< 15 yrs';
if PATIENT_AGE >=1 and PATIENT_AGE <=15 and pt_age_recode in ("years") then agegroup = '< 15 yrs';
if PATIENT_AGE >=16 and PATIENT_AGE <=34 and pt_age_recode in ("years") then agegroup = '16-34 yrs';
if PATIENT_AGE >=35 and PATIENT_AGE <=54 and pt_age_recode in ("years") then agegroup = '35-54 yrs';
if PATIENT_AGE >=55 and pt_age_recode in ("years") then agegroup = '55+ yrs';
run;
I don't think it's a problem with the SAS code, it's just that there are not enough conditional branches.
What I'm wondering is if there are records for age 0, 121-179 months, and 121-5474 days, the condition seems to be missing. If the data doesn't exist, it might not be a problem.
If you use if-then-do-end, or select-when-other, I think it will be a little more clear!
Please refer to the following
(This is untested, so please don't be offended)
/* use select */
select (pt_record);
when("newborn") do;
agegroup = '< 15 yrs';
end;
when("days") do;
if PATIENT_AGE <= 120 agegroup = '< 15 yrs';
end;
when("months") do;
if PATIENT_AGE <= 120 agegroup = '< 15 yrs';
end;
when("years") do;
select;
when( 1 <= PATIENT_AGE <= 15) agegroup = '< 15 yrs';
when(16 <= PATIENT_AGE <= 34) agegroup = '16-34 yrs';
when(35 <= PATIENT_AGE <= 54) agegroup = '35-54 yrs';
when(55 <= PATIENT_AGE ) agegroup = '55+ yrs';
otherwise;/* otherwise is same like else in if-else statements. But it should be written in the select statement, as it will cause errors for other conditions. */
end;
end;
otherwise;
end;
/* use if then else */
if pt_record = "newborn" then agegroup = '< 15 yrs';
else if pt_age_recode in ("years") then begin
if 1 <= PATIENT_AGE <= 15 then agegroup = '< 15 yrs';
else if 16 <= PATIENT_AGE <= 34 then agegroup = '16-34 yrs';
else if 35 <= PATIENT_AGE <= 54 then agegroup = '35-54 yrs';
else if 55 <= PATIENT_AGE then agegroup = '55+ yrs';
end else
if pt_record in ("months" "days") then begin
if PATIENT_AGE <= 120 agegroup = '< 15 yrs';
end;
Can you please post an excerpt of the data you have, so that we actually see what you start with? Most likely using a format is the best way to solve the problem.
So I'm still working on this question. I created a sample data for better understanding.
So this purpose is to identify age values in different 'agegroups' categories. For example,
0 is present in both years, months and days and I will like to have that in one group '< 15 yrs'
data test;
input patient_id patient_age patient_age_unit pt_age_code $;
datalines;
1231 24 2 months
1232 30 1 years
1233 3 1 years
1234 33 1 years
1235 0 0 newborn
1236 4 1 years
1237 0 1 years
1238 11 1 years
1239 90 1 years
1230 1 0 newborn
1212 0 2 months
1213 6 1 years
1214 11 2 months
1215 12 1 years
1216 0 3 days
1217 1 1 years
1218 34 1 years
1219 30 3 days
1220 1 2 months
1221 94 1 years
;
run;
I created this new codes but am not sure what else am doing wrong. Please help
data pt_age_grp (compress=yes keep= patient_id patient_age patient_age_unit pt_age_code agegroup );
set test;
if pt_age_recode = "newborn" then
do;
agegroup = '<15yrs';
end;
else if patient_age <=30 and pt_age_recode = "days" then
do;
agegroup = '<15yrs';
end;
else if patient_age <=11 and pt_age_recode = "months" then
do;
agegroup = '<15yrs';
end;
else if 1 <= patient_age <=15 and pt_age_recode = "years" then
do;
agegroup = '<15yrs';
end;
else if 16 <= patient_age <= 34 and pt_age_recode = "years" then
do;
agegroup = '16-34yrs';
end;
else if 35 <= patient_age <= 54 and pt_age_recode = "years" then
do;
agegroup = '35-54yrs';
end;
else if patient_age >= 55 and pt_age_recode = "years" then
do;
agegroup = '55+yrs';
end;
else do;
agegroup = 'Others';
end;
run;
If is not being evaluated correctly because pt_age_code and pt_age_recode are mixed up.
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.