If you have already created a macro variable named DATESTAMP with the value you want then there is no need to create it again inside the macro. You could just reference the existing macro variable. Or to make the macro more independent pass the value you already created into the macro as the value of another parameter. Or even better pass into the macro the name of the dataset you want to build and generate the name to pass in the call to the macro using the macro variable you already created in the larger program.
%extract(start=&startdt.,finish=&finishdt.,out=tk.application_extract_&datestamp.);
The macro code you posted is subsetting the data using the input parameters START and FINISH. So there is no need to create new macro variables named STARTMONTH and ENDMONTH. The only thing that the macro does with STARTMONTH and ENDMONTH is write them to the SAS log.
@Tom: I would never, ever use a macro variable declared outside the macro when the value should be derived from one of the macro parameters.
Unless I had a severe masochistic streak, of course.
If one decides to use an extra variable for storing the yyyymm timestamp, then it must be declared local to the macro, to avoid side-effects. Or you introduce it as an additional parameter, as you suggested.
Ok Guys
I have simplified the code as much as possible and run it, it ran without any errors..
Please advise if you are happy with this.
I have removed the datestamp variable inside the macro as I want to use datestamp with other programs as well.
%let startdt = 01JAN2016;
%let finishdt = 01FEB2017;
%let datestamp = %sysfunc(putn("&finishdt"d, yymmn6.));
%macro extract(start,finish);
data tk.application_extract_&datestamp.;
set sastrain.application_extract;
where "&startdt"d <= datepart(d_entry) <= "&finishdt"d;
run;
%mend extract;
%extract(&startdt.,&finishdt.);
I tried both statements with where and both worked , don't know why, If you could clarify.
where "&start"d <= datepart(d_entry) <= "&finish"d;
and
where "&startdt"d <= datepart(d_entry) <= "&finishdt"d;
see the difference in "&start"d and "&startdt"d, why these both are acceptable, don't know..
******************************************
I want to ask you Kurt you mention about
data _null_
and the symputx routines to do the same thing. Please advise for my learning
in which circumstances symputx is necessary and whats the advantage of that as compared
to the code that I used, I am sure they do the same thing but whats the advantage of using symputx
and why do we use it and prefer over the other code, just need to understand the difference and usage
of these routines.
%let startdt = 01JAN2016;
%let finishdt = 01FEB2017;
%let datestamp = %sysfunc(putn("&finishdt"d, yymmn6.));
%macro extract(start,finish);
data tk.application_extract_&datestamp.;
If datestamp should always be derived from finish, this is sloppy and bad programming, for reasons I have already stated.
This is only valid if finish as supplied as a macro parameter and datestamp as a global macro variable have no fixed relationship.
Regarding
where "&start"d <= datepart(d_entry) <= "&finish"d;
vs.
where "&startdt"d <= datepart(d_entry) <= "&finishdt"d;
comes from the fact that in your macro call you used startdt and finishdt as parameters, so the values are the same. If that is what it is planned to be, the whole macro definition is unnecessary, at least the parameters. (you might still create a macro if an identical piece of code is needed several times in your program)
Regarding the use of data steps and symput/symputx:
it allows me to create values with simple data step code, which (at least to me) is much easier (to create and to maintain) than doing it in macro code. The numerous problems that people encounter here on the communities when using macro language tells me that this is a valid point.
Also keep in mind that I am speaking from the POV of ~20 years of SAS experience and the POV of a communities superuser, which should tell you something. After all that time, I still only use macro code when I absolutely have to.
Hi Kurt
thanks for your reply , appreciate it,
so if you have to write this code from start to finish , how would you write it , say you are writing it in this macro format , how would you define the date stamp macros as global or local , please advise what would be your flow.
i am asking these silly questions as its my first attempt to write any macro, so be patient.
as far as the data steps and symput is concerned, that's way outside my scope at this stage, may be later on.
thanks
TK
"How would I write it"?
That depends on the issue at hand. What issue is to be solved?
From that, and the tools I have at hand, I create the basic logic design, and then I put that into code.
So the answer depends on the question above. What needs to be solved?
Then @Tom's post that you marked as solution (the final code piece) is what you are looking for.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.