DATA Step, Macro, Functions and more

How to Change a Variable Name Label to a date

Occasional Contributor
Posts: 19

How to Change a Variable Name Label to a date


I am creating a report every month and 2 of my columns are labelled "Current Months Rate" and "Previous Months Rate"  I was wondering if there was an easy way to actually display the date or even the Year and Month in place of  "Current Months Rate" or "Previous Months Rate".  So instead of "Current Months Rate" I would like the column to be labelled 03/01/2015 or 2015_03.  And I would need these columns to be relabelled automatically every month.

Any help would be much appreciated.


Super User
Posts: 13,508

Re: How to Change a Variable Name Label to a date

Is this from a procedure, if so which one (different options may be available). Is there a variable used to split the data into the two columns?

Would a solution based on the date the program is run be sufficient?

If you are starting with date values you may have a very easy solution but without knowing what your data looks like its not obvious if we should pursue that approach.

Occasional Contributor
Posts: 19

Re: How to Change a Variable Name Label to a date

I am joining several tables to get this report. I am using a master table to pull my last months numbers, using max date.  I am running my query again to get current months rate.  I am adding a Case function to compare the 2 columns.  After I run my compare, I want to change the labels to dates if possible.

Super User
Posts: 23,683

Re: How to Change a Variable Name Label to a date

I would keep my data in a long format until the 'final' dataset - which I don't recommend storing this way. When you need the data set this way you can use proc transpose to flip it with the IDLABEL option to ensure that the date is the label.

Super Contributor
Super Contributor
Posts: 3,176

Re: How to Change a Variable Name Label to a date

The SAS LABEL will need to be resolved with a double-quoted macro variable reference.  And you can use PROC SQL to select the SAS variable's data-value(s) into a SAS macro variable (for example, call it macro variable PERIOD) in a step ahead of the DATA / PROC step that sets the SAS statement, for illustration:

LABEL <myvar> = "Current Period (&period)";

Check the SAS PROC SQL DOC and SAS.COM resources for using INTO with SELECT <varname>  -- to create the SAS macro variable needed to resolve the various labels accurately.

Note that this can also be done in a preceding DATA step that reads the input file, with SET <filename> ( KEEP=<myvar>) END=EOF;   and then upon IF EOF THEN DO;  * SAS code;  END;  to issue CALL SYMPUT with a formatted SAS date variable to resolve the SAS macro variable.

Regular Contributor
Posts: 217

Re: How to Change a Variable Name Label to a date

This works.

proc sql;

drop table test;quit;

%let daterun=03/01/2015;

data test;

   label region = "&daterun.";

  set sashelp.demographics;

    keep region;


proc print data=test(obs=9) label; run;

Super User
Posts: 6,754

Re: How to Change a Variable Name Label to a date

A common way to automatically relabel every month is to use the INTNX function.

To illustrate, assume you can calculate the max date as being some day during April 2015.  Assume further that this means the "Current month" is March 2015 and the "Previous month" is February 2015.  (If the assumptions are incorrect, you can adjust the code pretty easily.)

Here is some code that captures the months/years as macro variables.

data _null_;

   * max_date is somehow previously calculated and brought in here;

   call symputx('current_my', put( intnx('month', max_date, -1), mmddyys10.));

   call symputx('prior_my', put( intnx('month', max_date, -2), mmddyys10.));


You can then use these macro variables as part of the labels when printing.

label current_month = "&current_my";

You will have the obligation to run the program during the correct month, otherwise you would need to change the code to get the proper column headings.

Good luck.

Ask a Question
Discussion stats
  • 6 replies
  • 6 in conversation