Hello everyone,
I am pretty new to sas programming and I am kind of stuck trying to move two variables as outfile-directory.
What I'm trying to do is filling up the filename of my exported text file with the last month and the current year.
So if someone runs the sas programme today the filename would be "filename-10-2014.txt"
I already have a code snippet which does work. But what it does, is inserting the whole current date into the filename:
%let Datum = &SYSDATE9;
export data=SASUSER.GEBDATUM
outfile= "X:\filename-&Datum..txt"
dbms=tab
replace;
Delimiter=';';
putnames=no;
;
Now I am trying to format the Date by splitting it up to a month and a year variable. Here is the complete code:
data datum;
Datum = &SYSDATE9;
Monat = MONTH(Datum);
Premonat =;
If Monat = '1' then Premonat = '12'; else Premonat = (Monat-1);
Jahr = YEAR(Datum);
%let mmyyyy = cat(-,Premonat,Jahr);
run;
export data=SASUSER.GEBDATUM
outfile= "X:\filename-&monat&mmyyyy..txt"
dbms=tab
replace;
Delimiter=';';
putnames=no;
run;
Here is the important part of the log:
NOTE: 9405 records were written to the file 'X:\filename-cat(-,Premonat,Jahr).txt'
So it does know that &mmyyyy is a variable within the code, but it does not transport the logic nor the value behind it.
What am I doing wrong here? What can I do to transport the values from the data step to my export procedure?
In the data step output my values are correct (Datum: 20047, Monat: 11, Premonat: 10, Jahr: 2014;)
Many thanks in advance.
I like to handle such problems with a data _null_ step:
data _null_;
datum = intnx('month',date(),-1);
call symput('mmyyyy',put(month(datum),z2.)!!put(year(datum),z4.));
run;
You are using the macro statement %let erroneously in the data step. Macro directives (%... statements and &.... variables) are executed by the macro processor BEFORE the data step is compiled and executed.
So you get a macro variable mmyyyy with the value cat(-,Premonat,Jahr)
Use call symput instead to export values from a data step into a macro variable.
I like to handle such problems with a data _null_ step:
data _null_;
datum = intnx('month',date(),-1);
call symput('mmyyyy',put(month(datum),z2.)!!put(year(datum),z4.));
run;
Your solution worked perfectly out.
Vielen Dank!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.