BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fre
Quartz | Level 8 fre
Quartz | Level 8

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%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

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

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.

Ksharp
Super User
%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
fre
Quartz | Level 8 fre
Quartz | Level 8

Thank you Ksharp & Patrick for giving me more insight into this matter.

Kurt_Bremser
Super User

@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

  1. do not apply the datepart() function to a date. datepart() is for extracting dates from datetimes
  2. you already specified a date literal, so no input function is necessary
  3. to display a date in a specified format within macro code, use the putn() function

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.

fre
Quartz | Level 8 fre
Quartz | Level 8

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?

Kurt_Bremser
Super User

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.));
fre
Quartz | Level 8 fre
Quartz | Level 8

Thank you Kurt, it's working now like it should.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 4890 views
  • 1 like
  • 4 in conversation