BookmarkSubscribeRSS Feed
Giuliano
Fluorite | Level 6


Hello,

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.

Thanks.

6 REPLIES 6
ballardw
Super User

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.

Giuliano
Fluorite | Level 6

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.

Reeza
Super User

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.

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

jwillis
Quartz | Level 8

This works.

proc sql;

drop table test;quit;

%let daterun=03/01/2015;

data test;

   label region = "&daterun.";

  set sashelp.demographics;

    keep region;

run;

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

Astounding
PROC Star

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.));

run;

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 4244 views
  • 0 likes
  • 6 in conversation