DATA Step, Macro, Functions and more

Macro date variable copy

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Macro date variable copy

Gday,

I have a small query about why a macro variable would behave differently in the following example:

16         %let WP1DB = %sysfunc(intnx(week, %sysfunc(today()), -1, b ) );
17         %let WP1DE = %sysfunc(intnx(week, %sysfunc(today()), -1, e ) );
18        
19         %put %sysfunc(putn(&WP1DB., date9.) ) %sysfunc(putn(&WP1DE., date9.) );
05APR2015 11APR2015
20        
21         %let dte_str = &WP1DB.;
22         %let dte_end = &WP1DE.;
23        
24         %put %sysfunc(putn(&dte_str., date9.) ) %sysfunc(putn(&dte_end., date9.) ) &dte_end.;
05APR2015 11APR2015
20189
25        
26         %let dte_str = "%sysfunc(putn(&WP1DB., date9.))"d;
27         %let dte_end = "%sysfunc(putn(&WP1DE., date9.))"d;
28        
29         %put %sysfunc(putn(&dte_str., date9.) ) %sysfunc(putn(&dte_end., date9.) ) &dte_end.;
05APR2015 11APR2015
"11APR2015"d
30        
31         %let dte_str = '05apr2015'd;
32         %let dte_end = '11apr2015'd;
33        
34         %put %sysfunc(putn(&dte_str., date9.) ) %sysfunc(putn(&dte_end., date9.) ) &dte_end.;
05APR2015 11APR2015
'11apr2015'd

I have an include statement after this and it runs differently, depending on which of the first two dte_end values I use.  I have set it up like this because sometimes I want the included job to be run on a monthly basis or a daily basis.

I think the second dte_end variable works as expected, it's the first one that doesn't.  I was wondering if there was a good reason for this?


I'm running SAS 9.3

Thanks


Accepted Solutions
Solution
‎04-13-2015 04:55 AM
Trusted Advisor
Posts: 3,212

Re: Macro date variable copy

SAS dates are defined as then number of days being passed since 1jan1960 SAS(R) 9.4 Language Reference: Concepts, Fourth Edition

The first construct is calculating those and storing  those in wp1dstr wp1dend as numbers exactly as of your instructions.

Adding the conversion to those numbers to be displayed as text is a chosen date format.

When using SAS datasets the test on dates with a number is no issue as it is still SAS.

When you put those numbers inside strings you are mixing up things.

When you want to use external datasets / databases that number is an issue as they are not SAS. The text date will do, but the SAS text date will not.

Having all thos in one let statement it would be like:

%let dte_str =  %sysfunc(putn( %sysfunc(intnx(week, %sysfunc(today()), -1, b ) )  , date9.)) ;
%let dte_end= %sysfunc(putn(  %sysfunc(intnx(week, %sysfunc(today()), -1, e ) )  , date9.)) ;

In planning situations (scheduling) and testing it is often more practical not to use the system-date today but one that can be set as an alternate day

For that some local standard being setup and to be followed will solve a lot of those date issues.

---->-- ja karman --<-----

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: Macro date variable copy

Sorry, am not clear on your question.  In the second two examples you have provided you put the text of a date into a string which contains quotes around it (single or double) and finish it with a d to indicate a date.  In the first example you resolve the function out to be a number, which is what dates are really in background.  So both 20189 and "11Apr2015"d are the same thing, just displayed differently. 

Secondly, why use macro variables to deal with dates?  Macro variables are text data which are used much like a find/replace command.  Dates are numeric data.  You can shoehorn one into the other bu putting dates, and input results, but you will just end up with messy code which is hard to debug.  Put you date paramters in a dataset, then you can use merging to use that with your code, and you have full functionality of date processing within a datastep, e.g.

proc sql;

     create table WANT as

     select     A.JOB_ID,

                   A.JOB_DATE

     from       JOBS A

     where     A.JOB_DATE < (select ACTION_DATE from PARAMETER_TABLE);

quit;

Solution
‎04-13-2015 04:55 AM
Trusted Advisor
Posts: 3,212

Re: Macro date variable copy

SAS dates are defined as then number of days being passed since 1jan1960 SAS(R) 9.4 Language Reference: Concepts, Fourth Edition

The first construct is calculating those and storing  those in wp1dstr wp1dend as numbers exactly as of your instructions.

Adding the conversion to those numbers to be displayed as text is a chosen date format.

When using SAS datasets the test on dates with a number is no issue as it is still SAS.

When you put those numbers inside strings you are mixing up things.

When you want to use external datasets / databases that number is an issue as they are not SAS. The text date will do, but the SAS text date will not.

Having all thos in one let statement it would be like:

%let dte_str =  %sysfunc(putn( %sysfunc(intnx(week, %sysfunc(today()), -1, b ) )  , date9.)) ;
%let dte_end= %sysfunc(putn(  %sysfunc(intnx(week, %sysfunc(today()), -1, e ) )  , date9.)) ;

In planning situations (scheduling) and testing it is often more practical not to use the system-date today but one that can be set as an alternate day

For that some local standard being setup and to be followed will solve a lot of those date issues.

---->-- ja karman --<-----
Frequent Contributor
Posts: 75

Re: Macro date variable copy

Thanks for the response.

Honestly I'm not sure if your response is correct, I'm not doing a text/string comparison.  It sounds reasonable though.

I'll be sure to try your suggested code, but am not sure if that's how I want my jobs to run.

Trusted Advisor
Posts: 3,212

Re: Macro date variable copy

Think on how you want your jobs to be run. What times automated handed over to others etc.
At one time I did set a standard for users offering al lot of those macro-vars (current week last week,  current mont lat month as all kind)  in macro text-string with a simulation date for time-travelling.
That is very advanced and may not according your needs.

---->-- ja karman --<-----
🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 308 views
  • 3 likes
  • 3 in conversation