Please help me with the following code, I'm using SAS 9.3. I'm trying to create age categories for children:
Birth up to 1
1-5 years old
6-12 years old
13 up to 18
data new; set old;
if subject_age=0 or age_unit in ("Hours","Days","Months") then age_cat=1;
if 1<=subject_age<=5 and age_unit in ("Years") then age_cat=2;
if 6<=subject_age<=12 and age_unit in ("Years") then age_cat=3;
if 13<=subject_age<=17 and age_unit in ("Years") then age_cat=4;
run;
Each time I run the code, all observations are coded as age_cat=1
Almost anything involving age or date-duration elements it is often best practice to turn things into a standard unit, all days, weeks, months what have you.
Your references two variables, subject_age and age_unit, but you do not show any example data values. You may want to provide some examples that are assigned age_cat1 that you think are incorrect.
With some of my dummy data I do not get any unexpected results.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
Please post some sample data, specifically ones that replicate your issue.
Note that this does not need to be real data but should reflect your situation.
I made up some data, and I'm not having this problem.
Show us (part of) your data.
Almost anything involving age or date-duration elements it is often best practice to turn things into a standard unit, all days, weeks, months what have you.
Your references two variables, subject_age and age_unit, but you do not show any example data values. You may want to provide some examples that are assigned age_cat1 that you think are incorrect.
With some of my dummy data I do not get any unexpected results.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
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.