Creating Age Groups from Age Variable

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Creating Age Groups from Age Variable

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


Accepted Solutions
Solution
‎11-27-2017 01:08 PM
Super User
Posts: 13,584

Re: Creating Age Groups from Age Variable

Posted in reply to katesmith

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


All Replies
Super User
Posts: 23,785

Re: Creating Age Groups from Age Variable

Posted in reply to katesmith

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.

Respected Advisor
Posts: 3,069

Re: Creating Age Groups from Age Variable

Posted in reply to katesmith

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

 

Show us (part of) your data.

--
Paige Miller
Solution
‎11-27-2017 01:08 PM
Super User
Posts: 13,584

Re: Creating Age Groups from Age Variable

Posted in reply to katesmith

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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