Reputation: 27
I'm brand new to SAS and working from someone else's code as a way of teaching myself, so bear with me if the premise of my question is extremely basic. Also, I've seen a similar question asked elsewhere but not helpful for my particular circumstance given my inexperience with SAS, unfortunately. In any case, here's an overview of what I'm trying to do.
I'm trying to adjust existing code at my workplace to generate a new file every single day, but to append the current day's date to each file so that no files are overwritten and historic data are captured and retained. The following code is what I'm trying to achieve, essentially:
Data output_file_[current date];
set input_file;
run;
I've also tried to use a macro, like below:
%let date = %sysfunc(today(), mmddyyd10.);
Data output_file_&date;
set input_file;
run;
These haven't worked, unfortunately. If anybody has alternative thoughts that might be helpful here, please let me know!
Upvotes: 0
Views: 461
Reputation: 27498
A SAS session has an automatic global macro variable, SYSDATE, that you can use. The value is a SAS date literal representation ddmonyy (i.e. 08NOV23).
The value represents the date when the SAS session started and does not roll to the next day at midnight.
Example:
data want_&SYSDATE ;
set sashelp.class ;
run;
will log
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT_08NOV23 has 19 observations and 5 variables.
Upvotes: 1
Reputation: 51566
Don't use dates in either MDY or DMY order for something like this. Not only will either choice confuse half of the users looking at the resulting values they will not sort in chronological order. Using YMD order when sticking date strings into names will allow the names to sort properly and avoid having the answer the question: Is that the tenth of December or November twelfth.
You can use the YYMMDDN8. format to generate strings of only digits.
Or you can use YYMMDDD10. format to generate strings with dashes and then convert the dashes into underscores.
Example:
1 %let ymd=%sysfunc(date(),yymmddn8.);
2 %put &=ymd;
YMD=20231107
3 %let ymd_dash=%sysfunc(date(),yymmddd10.);
4 %let ymd_underscore=%sysfunc(translate(&ymd_dash,_,-));
5 %put &=ymd_underscore;
YMD_UNDERSCORE=2023_11_07
Upvotes: 1
Reputation: 1394
When you actually use mmddyyd10.
to format a date value, you'll get something like 11-08-2023
, it is not only a readable date value, but also an invalid part of SAS dataset name.
Strictly, if you don't modify system option VALIDMEMNAME=
, 3 rules make an invalid SAS dataset name:
So 20231108
or 08NOV2023
is OK as a part of a data set name, but 11-08-2023
is not, because the hypens are invalid elements. To use 20231108
or 08NOV2023
, you can use format yymmddn8.
or date9.
.
Ex:
%let date = %sysfunc(today(), yymmddn8.);
Data output_file_&date;
set input_file;
run;
Upvotes: 2