SAS (Base SAS)+
* Create format using PROC FORMAT;
proc format;
value sexfmt
1 = "Male"
2 = "Female"
;
run;
data dm;
input usubjid $ sex;
datalines;
01 1
02 2
03 1
04 2
;
run;
* Apply the format and create labeled variable;
data dm_labeled;
set dm;
sexc = put(sex, sexfmt.);
run;- In SAS, the code-to-label mapping is declared with
proc formatand then applied withput(). proc format; value sexfmt 1 = "Male" 2 = "Female"; run;defines a reusable format calledsexfmt.- The format is stored in the current format catalogue and is available to any DATA step or PROC until the session ends.
sexc = put(sex, sexfmt.);translates the numeric code into its label — the trailing dot onsexfmt.tells SAS it is a user-defined format.- Unlike a merge-lookup, the format costs almost nothing to evaluate row by row and stays attached to the column as metadata.
- We run and open DM_LABELED; the new
sexccolumn should read "Male" for sex = 1 and "Female" for sex = 2.