I'm having trouble understanding the following code, specifically starting with label="+"||SS_year;
i think label="+" means make a variable called "label" and populate it with "+", but I'm not sure what the "||SS_year" comes in.
Also, what does fmtname="$all_pts_prev"; mean? Make a variable called "fmtname" that is populated by "$all_pts_prev"?
Relevant code below:
data fmt_all_yrs;
set interim.sample;
if SS_2010=1 then SS_year='2010';
else if SS_2011=1 then SS_year='2011';
else if SS_2012=1 then SS_year='2012';
else if SS_2013=1 then SS_year='2013';
else if SS_2014=1 then SS_year='2014';
start=strip(patient_id);
label="+"||SS_year;
fmtname="$all_pts_prev";
keep start label fmtname;
run;
And code immediately after this says
proc sort nodupkey data=fmt_all_yrs
out=format.fmt_all_pts_prev;
by start;
run;
proc format cntlin=format.fmt_all_pts_prev library=format;
run;
Is this saying we're going to sort a dataset called fmt_all_yrs and output it into the library format, and call the new dataset "fmt_all_pts_prev" and organize it by the variable "start"...
Not sure what the last line means though (proc format cntlin=format.fmt_all_pts_prev library=format;)
|| concatenates character strings.
The code as a whole is creating a data set that will create a format--instructions for rendering a value in a data set in a particular way. SAS provides many formats. The 5.2 format will display 33.33333 is 33.33. However, many times you want explicit ad hoc instructions such as display 'M' as 'Male' and display 'F' as 'Female'. Look at the PROC FORMAT documentation and you will see examples of other CNTLIN= data set that make formats. Values of your variable that match Start should instead be displayed as the value of Label.
|| concatenates character strings.
The code as a whole is creating a data set that will create a format--instructions for rendering a value in a data set in a particular way. SAS provides many formats. The 5.2 format will display 33.33333 is 33.33. However, many times you want explicit ad hoc instructions such as display 'M' as 'Male' and display 'F' as 'Female'. Look at the PROC FORMAT documentation and you will see examples of other CNTLIN= data set that make formats. Values of your variable that match Start should instead be displayed as the value of Label.
Thank you! Super helpful and learning so much 🙂
Two questions below.
1. So the $all_pts_prev doesn't seem to be a SAS format... does that mean that this coder made a new format called "$all_pts_prev" and stored it in a dataset called "fmt_all_pts_prev"?
How could I set up such a format dataset myself? I don't have access to the original dataset so I'm trying to figure out what's going on here...
proc sort nodupkey data=fmt_all_yrs
out=format.fmt_all_pts_prev;
by start;
run;
proc format cntlin=format.fmt_all_pts_prev library=format;
run;
2. What do you mean by "values of your variable that match Start should instead be displayed as the value of Label."?
I think it would be good to look at the CNTLIN= example in the doc.
$all_pts_prev is not a format SAS provides. It is the one you are making.
Like I said, your variable might have values like 1 or M for Male or 2 or F for Female. So you can write a format to display Male and Female instead of the less informative values.
Even if you don't have the original data, print the CNTLIN data set. The start values are like my 1, M or 2, F values. The label values are like my Male and Female values. Formats enable you to display custom values for any (typically more parsimonious values).
Here is one of my blogs. It begins with a vanilla example of making a format by using a VALUE statement, then it shows the CNTLIN= data set approach. I use both regularly in my work. It just depends on whether it is more convenient to create a data set or a statement. The content of the blog may be a bit beyond what you are looking for, but the technique is the same regardless of the problem.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.