DATA Step, Macro, Functions and more

Setting a calculated value as a Global Variable

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Setting a calculated value as a Global Variable

Hello all,

I have been working on some code and would like to add this to the beginning of my program to avoid doing any calculations of time so it runs independently on weekend/off work schedule. So far it is set to run on Sundays but when I run it during the week I have to manually calculate the timeframe needed. The following is just an example of what I need and it is meant to calculate the prior month end day (last day of previous month) and the start of the month six whole months prior:

 

data MONTH;

CurrentDay=today();

PriorMOEnd = CurrentDay- (DAY(CurrentDay));

PriorMOStart = intnx('MONTH',PriorMOEnd - (day(PriorMOEnd)-1),-5) ;

format CurrentDay PriorMOEnd PriorMOStart DATE8.;

run;

proc print data=MONTH;

run;

DATA TEST;

SET MONTH;

%Global START END;

%LET START = PriorMOEnd;

%LET END = PriorMOStart;

RUN;

DATA TEST2;

X=&START;

Y=&END;

RUN;

The second and third data steps are just to test the setting of the global variables but when DATA TEST 2 runs there is nothing there. I've searched for a good resource for this question for the past few days with no luck and would appreciate any and all comments. I should mention that I am new to SAS but have some experience with SQL and learning both the SAS and PROC SQL programming steps.

Thank you!


Accepted Solutions
Solution
‎08-27-2015 11:24 AM
Super User
Posts: 11,336

Re: Setting a calculated value as a Global Variable

The interface to create a macro variable in a data step are the functions Call Symput and Call Symputx.

Since macro variables contain text to you want the Start and End to have the SAS date numeric value, which would look like  20327 or something more human readable like 08/27/2015?

If you are using the results to filter on SAS date values the former would likely work best.


data _null_;
CurrentDay=today();
PriorMOEnd = CurrentDay- (DAY(CurrentDay));
PriorMOStart = intnx('MONTH',PriorMOEnd - (day(PriorMOEnd)-1),-5) ;
format CurrentDay PriorMOEnd PriorMOStart DATE8.;
call symputx ('Start',PriorMOEnd);
call symputx ('End',PriorMOStart);
run;

%put Start= &start  End= &end;

If you want the values to be more readable then  something like
call symputx ('Start', put(PriorMOEnd,Date8.));

By default I believe that these will be global.

View solution in original post


All Replies
Solution
‎08-27-2015 11:24 AM
Super User
Posts: 11,336

Re: Setting a calculated value as a Global Variable

The interface to create a macro variable in a data step are the functions Call Symput and Call Symputx.

Since macro variables contain text to you want the Start and End to have the SAS date numeric value, which would look like  20327 or something more human readable like 08/27/2015?

If you are using the results to filter on SAS date values the former would likely work best.


data _null_;
CurrentDay=today();
PriorMOEnd = CurrentDay- (DAY(CurrentDay));
PriorMOStart = intnx('MONTH',PriorMOEnd - (day(PriorMOEnd)-1),-5) ;
format CurrentDay PriorMOEnd PriorMOStart DATE8.;
call symputx ('Start',PriorMOEnd);
call symputx ('End',PriorMOStart);
run;

%put Start= &start  End= &end;

If you want the values to be more readable then  something like
call symputx ('Start', put(PriorMOEnd,Date8.));

By default I believe that these will be global.

New Contributor
Posts: 2

Re: Setting a calculated value as a Global Variable

Thank you very much Ballardw!!

I was playing around with the Symput and Symget functions but you put it in such simpler terms that it was very easy to follow. Thank you very much.

If someone does use the above example for reference, I would change the following:

data _null_;

CurrentDay=today();

PriorMOEnd = CurrentDay- (DAY(CurrentDay));

PriorMOStart = intnx('MONTH',PriorMOEnd - (day(PriorMOEnd)-1),-5) ;

format CurrentDay PriorMOEnd PriorMOStart DATE8.;

call symputx ('Start',PriorMOStart);

call symputx ('End',PriorMOEnd);

run;

%put Start= &start  End= &end;

If you want the values to be more readable then  something like
call symputx ('Start', put(PriorMOEnd,Date8.));

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 452 views
  • 1 like
  • 2 in conversation