BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

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.

Kurt_Bremser
Super User

@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 you use the macro in a program that does not have &datestamp, it crashes
  • if global &datestamp is calculated from other values than those used in calling the macro, hilarious fun ensues.

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.

bondtk
Quartz | Level 8

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. 

 

 

Kurt_Bremser
Super User
%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.

 

bondtk
Quartz | Level 8

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

 

 

Kurt_Bremser
Super User

"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?

bondtk
Quartz | Level 8
Issue to be solved:

Write a data extraction macro from start to finish date:

And we need to stamp the file name whenever the the data is run instead of naming the file manually.

And may be we don’t need the start date , we just need the date on which it’s run and picks all the data for that date going back.

TK

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 22 replies
  • 3462 views
  • 4 likes
  • 4 in conversation