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

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 😞 Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

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.

 

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
  • 2 replies
  • 6298 views
  • 2 likes
  • 3 in conversation