BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
katesmith
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

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.

PaigeMiller
Diamond | Level 26

I made up some data, and I'm not having this problem.

 

Show us (part of) your data.

--
Paige Miller
ballardw
Super User

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2550 views
  • 0 likes
  • 4 in conversation