Search code examples
sassas-macro

Populate data step name with today's date in SAS


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!


Solution

  • 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:

    1. The length of the names can be up to 32 characters.
    2. Names must begin with a letter from the Latin alphabet (A–Z, a–z) or an underscore. Subsequent characters can be letters from the Latin alphabet, numerals, or underscores.
    3. Names cannot contain blanks or special characters except for the underscore.

    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;