BookmarkSubscribeRSS Feed
lolol0101
Calcite | Level 5

Hello, I'm still starting out on SAS so please bear with me.

 

How do I use proc format “YOL” into the each respective years: “0-12 years”, “13-24 years”, “25-36 years”, “36-60 years” and “61 years+”

 

I have them saved in a dataset under the variable "YOL" (Years of Loyaltyy).

 

 

3 REPLIES 3
Kurt_Bremser
Super User

Create a format for the "age" groups. Then use it in statistic procedures for the "age" variable. The procedures will use the formatted value for the groups.

 

For detailed help, show your (intended) analysis code.

PaigeMiller
Diamond | Level 26

since we don't have your data (you should really provide example data with your questions), here is an example using SASHELP.CARS

 

proc format;
    value msrpf 0-<10000='$0-$10K' 10000-<20000='$10K-$20K'
        20000-<30000='$20K-$30K' 30000-high='>=$30K';
run;

proc means data=sashelp.cars;
    var horsepower;
    class msrp;
    format msrp msrpf.;
run;
--
Paige Miller
Rick_SAS
SAS Super FREQ

The beauty of using PROC FORMAT is that you do not need to create a new variable that contains the bin information; you simply apply the format to an existing variable. Examples and a discussion are given in "5 reasons to use PROC FORMAT to recode variables in SAS."

 

I think the following example might suit your needs:

data Have;
input Months @@;
RawMonts = Months;
datalines;
0 11 12 13 23 24 25 35 36 37 59 60 61 73
11.9 12.1 13.3 23.7 24.2
;

/* create a YOL format */
proc format;
value YOLFmt  
       low -  12  = "0-12 Months"    /* <= 12    */
       12 <-  24  = "13-24 months"   /* (12, 24] */
       24 <-  36  = "25-36 months"   /* (24, 36] */
       36 <-  60  = "36-60 months"  
       60 <- high = "61+ months";   
run;

/* apply the format */
proc print data=Have;
   format Months YOLFmt.;
run;

/* apply the format in a computational procedure */
proc freq data=Have;
   format Months YOLFmt.;
   tables Months / nocol norow nopercent;
run;

Although it is rarely necessary, you can also use the format to create a NEW variable:

 

/* if necessary, you can create a new variable, but it is rarely necessary */
data Want;
set Have;
length YOL $12;
YOL = put(Months, YOLFmt.);
run;

proc print data=Want;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1456 views
  • 2 likes
  • 4 in conversation