BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cedpme
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

cedpme
Calcite | Level 5

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.));

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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