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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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