DATA Step, Macro, Functions and more

Creating Age Groups from Age Variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Creating Age Groups from Age Variable

Hello!

 

I am new to programming and need to create new "group" variables based on ages. I am doing this because I will eventually need to run an ANOVA test comparing three age groups against each other. I have tried to create new variables from the one variable of age but havent had any luck.

 

I used proc import for my data from an excel file, although I am not entirely sure this is right, it has worked so far:

PROC IMPORT OUT= SASUSER.RSV
DATAFILE= "H:\windows\IP\RSV\rsv.xls"
DBMS=EXCELCS REPLACE;
RANGE="Sheet1$";
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;

RUN;

 

So I need three age groups (0-3, 4-6, 7-12 months) from one variable currently titled AGE

 

I have already used proc format for a frequency table but will need something more permanent ( I think)

proc format;
value age_fmt
0-3='0-3 Months' 4-6='4-6 Months' 7-12='7-12 Months';
value GA_fmt
24-36='Preterm'37-42='Term';
run;

 

Please help Smiley Sad Thanks!


Accepted Solutions
Solution
‎02-09-2017 04:31 PM
Super User
Posts: 11,343

Re: Creating Age Groups from Age Variable

Posted in reply to edomachowske

There is nothing wrong with what you are attempting. However placing your data in the SASUSER library is not a best practice. Your SASUSER can be completely replaced by a SAS upgrade an your data sets are not going to survive. Better would be to create your own permanent library. Then place the data there, put your custom formats there and that that library to the format search path.

 

Adding the code for the library assignment and the search path to an AUTOEXEC.SAS file called when SAS starts is one way to have things persist. These two lines could be in the autoexec.sas to assign everytime sas starts and adds that library to the

Libname mylib "C:\path\libfolder";  /* use your peferred library name, helpful if it means something 8 character limit*/
options append=(fmtsearch= mylib);

This would place the information for the formats into that library.

 

proc format libary=mylib cntlout=mylib.FmtCntlout;
value age_fmt
0-3='0-3 Months' 
4-6='4-6 Months' 
7-12='7-12 Months';
value GA_fmt
24-36='Preterm'
37-42='Term';
run;

Then import the data into that that library.

 

With the format in the permanent library and the search path you can use it without rerunning the proc format code each time. This makes permanenly assigning the format to the variables possible and most procedures will then use the formats, or you can explicitly reference the format when desired and SAS will find it.

 

The cntlout option creates a data set that you can recreate the formats from or inspect settings if the code gets lost.

View solution in original post


All Replies
Solution
‎02-09-2017 04:31 PM
Super User
Posts: 11,343

Re: Creating Age Groups from Age Variable

Posted in reply to edomachowske

There is nothing wrong with what you are attempting. However placing your data in the SASUSER library is not a best practice. Your SASUSER can be completely replaced by a SAS upgrade an your data sets are not going to survive. Better would be to create your own permanent library. Then place the data there, put your custom formats there and that that library to the format search path.

 

Adding the code for the library assignment and the search path to an AUTOEXEC.SAS file called when SAS starts is one way to have things persist. These two lines could be in the autoexec.sas to assign everytime sas starts and adds that library to the

Libname mylib "C:\path\libfolder";  /* use your peferred library name, helpful if it means something 8 character limit*/
options append=(fmtsearch= mylib);

This would place the information for the formats into that library.

 

proc format libary=mylib cntlout=mylib.FmtCntlout;
value age_fmt
0-3='0-3 Months' 
4-6='4-6 Months' 
7-12='7-12 Months';
value GA_fmt
24-36='Preterm'
37-42='Term';
run;

Then import the data into that that library.

 

With the format in the permanent library and the search path you can use it without rerunning the proc format code each time. This makes permanenly assigning the format to the variables possible and most procedures will then use the formats, or you can explicitly reference the format when desired and SAS will find it.

 

The cntlout option creates a data set that you can recreate the formats from or inspect settings if the code gets lost.

Super User
Posts: 5,503

Re: Creating Age Groups from Age Variable

Posted in reply to edomachowske

It's conceivable that you are actually asking a simpler set of questions.  Given all the work you have done so far, why aren't the formats being used?  And how can  you create variables that group according to the formats?

 

Creating formats doesn't actually apply them to any variables.  There are two ways to apply formats:  (1) associate a format with a variable (either permanently or temporarily), and (2) create a new variable by using the format. 

 

That second step is something that is fairly easy given the work you have done so far:

 

data want;

set have;

age_grouping = put(age, age_fmt.);

ga_grouping = put(age, ga_fmt.);

run;

 

That adds two character variables to your data set (AGE_GROUPING and GA_GROUPING), holding the results of grouping the original AGE values by applying your formats.  As you can see, there are values of AGE that are not defined by the formats.  You might want to change that (adding to the format definitions), because the new variables holding the grouped versions will reflect the unformatted values of AGE.  Take a look at the results and you'll see what I'm talking about.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 958 views
  • 2 likes
  • 3 in conversation