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).
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.
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;
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;
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.
Ready to level-up your skills? Choose your own adventure.