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