SAS Users

Creating and Using Multilabel Formats

A multilabel format enables you to assign multiple labels to a value or a range of values. The capability to add multilabel formats was added to the FORMAT procedure in SAS® 8.2. You assign multiple labels by specifying the MULTILABEL option in the VALUE statement of PROC FORMAT. For example, specifying the MULTILABEL option in the following VALUE statement enables the Agef format to have overlapping ranges.

value agef (multilabel)
11=’11’
12=’12’
13=’13’
11-13=’11-13′;

Multilabel formats are available for use only in the MEANS, SUMMARY, TABULATE, and REPORT procedures. The code examples that follow show the creation of a simple mutlilabel format (using PROC FORMAT) and its use in each of these procedures.

First, a PROC FORMAT step creates a multilabel format for the Age variable in the Sashelp.Class data set, along with a character format for the Sex variable. The NOTSORTED option is specified to indicate the preferred order of the ranges in the results.

proc format library=work;
value agef (multilabel notsorted)
11=’11’
12=’12’
13=’13’
11-13=’11-13′
14=’14’
15=’15’
16=’16’
14-16=’14-16′;
value $sexf
‘F’=’Female’
‘M’=’Male’;
run;

Now, the multilabel format is used in the other SAS procedures that are mentioned earlier. In PROC MEANS and PROC TABULATE, the MLF option must be specified in the CLASS statement for the Age variable. In PROC REPORT, the MLF option is specified in the DEFINE statement for Age. The PRELOADFMT and ORDER=DATA options are also specified to preserve the order as defined in the format. The PRELOADFMT option applies only to group and across variables in PROC REPORT.

proc tabulate data=sashelp.class format=8.1;
class age / mlf preloadfmt order=data;
class sex;
var height;
table age, sex*height*mean;
format age agef. sex $sexf.
title ‘PROC TABULATE’;
run;
proc means data=sashelp.class nway Mean nonobs maxdec=1 completetypes;
class age / mlf preloadfmt order=data;
class sex;
var height;
format age agef. sex $sexf.;
title ‘PROC MEANS’;
run;

proc report data=sashelp.class NOWD headline completerows;
col age sex height;
define age / group mlf preloadfmt order=data format=agef.;
define sex / group format=$sexf.;
define height / mean format=8.1;
break after age / skip;
title ‘PROC REPORT’;
run;

The output from each of these procedures is shown below.

Creating and Using Multilabel Formats

Creating and Using Multilabel Formats

You can use a multilabel format to facilitate the calculation of moving averages, as illustrated in the next example. This example creates a multilabel format using the CNTLIN= option in PROC FORMAT. Then, that format is used to calculate a three-month moving average in PROC SUMMARY.

data sample; /* Create the sample data set. */
do sasdate=’01JAN2015’D to ’31DEC2016’D;
x=ranuni(20089)*1234;
if day(sasdate)=1 then output;
end;
run;

proc print data=sample;
format sasdate date9.;
title ‘Sample data set’;
run;

data crfmt; /* Create a CNTLIN data set for a multilabel format. */
keep fmtname start end label HLO;
begin=’01JAN2015’D;
final=’31DEC2016’D;
fmtname=’my3month’;
periods=intck(‘month’,begin,final) -2;
do i=0 to periods;
end=intnx(‘month’,final,-i,’E’);
start=intnx(‘month’,end,-2);
label=catx(‘-‘,put(start,date9.),put(end,date9.));
HLO=’M’; /* M indicates “multilabel.” */
output;
end;
run;

proc print data=crfmt;
var fmtname start end label HLO;
format start end date9.;
title ‘CNTLIN data set’;
run;

/* Use the CNTLIN= option to create the format. */
proc format library=work cntlin=crfmt fmtlib;
select my3month;
title ‘FMTLIB results for my3month format’;
run;

proc summary data=sample NWAY order=data;
class sasdate / MLF; /* Use the MLF option. */
var x;
output out=final (drop=_: ) mean= / autoname;
format sasdate my3month.;
run;

proc print data=final noobs;
title ‘Three-Month Moving Averages’;
run;

The example code above generates the following output:

Creating and Using Multilabel Formats

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s