Hi,
I would like to add a date to the name of a dataset.
This date (reference_date) has been defined somewhere in the beginning of my code.
In the example in the following code, I would like to have a tablename like "RESULTS_2005_010519"
The following code did not work, what could be wrong with it?
%macro backup_results;
%let reference_date= '1MAY2019'd;
/* other code */
data test.RESULTS_2005_%sysfunc(datepart(%sysfunc(input(&reference_date.,DDMMYY10.))));
set test.RESULTS_2005;
run;
%mend;
%macro backup_results;
%let reference_date= '1MAY2019'd;
/* other code */
data RESULTS_2005_%sysfunc(putn(&reference_date.,DDMMYYn6.));
set sashelp.class;
run;
%mend;
%backup_results
The string....
'1MAY2019'd
...doesn't convert to a SAS date the way you pass it to the SAS macro variable. It's just exactly the string. For this reason your %sysfunc() stuff is not going to work as it expects a SAS data value.
Also: If it would be a SAS date value then there is no reason to use the datepart() function.
Question: Is the string as passed to &reference_date a given or can you change this? Your answer will drive where the code change needs to be applied.
Here an approach if you can't change &reference_date
%let reference_date= '01MAY2019'd;
data _null_;
call symputx('reference_date_2',&reference_date);
stop;
run;
%put &=reference_date_2;
data test_2005_%sysfunc(putn(&reference_date_2.,DDMMYYn8.));
set sashelp.class;
run;
proc contents data=_last_;
run;
Actually true, @Ksharp is right. Using the string in the putn() function is already enough for SAS to first convert it to a SAS date value.
%macro backup_results;
%let reference_date= '1MAY2019'd;
/* other code */
data RESULTS_2005_%sysfunc(putn(&reference_date.,DDMMYYn6.));
set sashelp.class;
run;
%mend;
%backup_results
Thank you Ksharp & Patrick for giving me more insight into this matter.
@fre wrote:
Hi,
I would like to add a date to the name of a dataset.
This date (reference_date) has been defined somewhere in the beginning of my code.
In the example in the following code, I would like to have a tablename like "RESULTS_2005_010519"
The following code did not work, what could be wrong with it?
%macro backup_results; %let reference_date= '1MAY2019'd; /* other code */ data test.RESULTS_2005_%sysfunc(datepart(%sysfunc(input(&reference_date.,DDMMYY10.)))); set test.RESULTS_2005; run; %mend;
Multiple things
Try this:
%let reference_date= '1MAY2019'd;
%put %sysfunc(putn(&reference_date.,DDMMYY10.));
But I would strongly recommend, when time-stamping files or datasets, to use the ymd order for your date:
data test.RESULTS_2005_%sysfunc(putn(&reference_date.,yymmddd10.));
set test.RESULTS_2005;
run;
because then everything sorts chronologically on its own.
Also note that I put dashes into the timestamp instead of slashes (that's the third "d" in the format), as that won't confuse your filesystem syntax.
Thank you Kurt for your clarification.
I can understand your advice of using the ymd order.
Therefore, I've adapted my code from:
data test.RESULTS_2005_%sysfunc(putn(&reference_date.,DDMMYYn6.)); /* solution provided by Ksharp, which works */
to:
data test.RESULTS_2005_%sysfunc(putn(&reference_date.,yymmddd10.)); /* solution by Kurt => error */
The error I get after this change is ERROR 22-322:
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.
76: LINE and COLUMN cannot be determined.
I do not understand why changing the format from DDMMYYn6. to yymmddd10. induces this error. Do you?
My bad.One can't have dashes in dataset names, only in filenames. Remove the dashes altogether:
data test.RESULTS_2005_%sysfunc(putn(&reference_date.,yymmddn8.));
Thank you Kurt, it's working now like it should.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.