Help using Base SAS procedures

Variable in PROC EXPORT outfile

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Variable in PROC EXPORT outfile


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: 2014Smiley Wink

Many thanks in advance.


Accepted Solutions
Solution
‎11-20-2014 07:23 AM
Super User
Posts: 7,782

Re: Variable in PROC EXPORT outfile

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 7,782

Re: Variable in PROC EXPORT outfile

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎11-20-2014 07:23 AM
Super User
Posts: 7,782

Re: Variable in PROC EXPORT outfile

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 2

Re: Variable in PROC EXPORT outfile

Posted in reply to KurtBremser

Your solution worked perfectly out.


Vielen Dank!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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