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


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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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;

ins_dev
Calcite | Level 5

Your solution worked perfectly out.


Vielen Dank!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 6187 views
  • 3 likes
  • 2 in conversation