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

 

I am creating a macro variable for dates such that 

 

%let change_date= '2018-07-01'

%put &change_date

 

%let interval = 15 (or 30 or 45)

 

 

and it should give me a macro variable with dates

 

%let previous_end_date = '2018-06-30' (This is change_date -1)

%let previous_start_date= '2018-06-16' (this is change_date -15)

 

%let current_start_date= '2018-07-02' (This is change_date+1)

%let current_end_date= '2018-07-16' (This is change_date+15)

 

I want the dates to be in this format as this is how Teradata accept the date values. Can you help me out on how to do this macro's calculation?

Any help is highly appreciated.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Use CALL SYMPUT logic to read the date values into macro variables like this

 

data _null_;
   change_date= '01jul2018'd;

   previous_end_date=change_date-1;
   previous_start_date=change_date-15;

   current_start_date=change_date+1;
   current_end_date=change_date+15;

   call symputx('previous_end_date',previous_end_date);
   call symputx('previous_start_date',previous_start_date);
   call symputx('current_start_date',current_start_date);
   call symputx('current_end_date',current_end_date);

   put (_ALL_)(=);
   format _numeric_ date9.;
run;

%put _USER_;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data _null_;
   change_date= '01jul2018'd;

   previous_end_date=change_date-1;
   previous_start_date=change_date-15;

   current_start_date=change_date+1;
   current_end_date=change_date+15;

   put (_ALL_)(=);
   format _numeric_ date9.;
run;
PeterClemmensen
Tourmaline | Level 20

If it is absolutely necessary to put them into macro variables, use call symput logic.

hvempati
Obsidian | Level 7

Thank you @PeterClemmensen for the answer. While I appreciate your answer, will those variables in Data be converted into Macro Variable?

If not, I give these variables in one program in Enterprise guide and use it in different programmes as well. Can these values be transferred to different programmes as well?

 

Thank you.

PeterClemmensen
Tourmaline | Level 20

Use CALL SYMPUT logic to read the date values into macro variables like this

 

data _null_;
   change_date= '01jul2018'd;

   previous_end_date=change_date-1;
   previous_start_date=change_date-15;

   current_start_date=change_date+1;
   current_end_date=change_date+15;

   call symputx('previous_end_date',previous_end_date);
   call symputx('previous_start_date',previous_start_date);
   call symputx('current_start_date',current_start_date);
   call symputx('current_end_date',current_end_date);

   put (_ALL_)(=);
   format _numeric_ date9.;
run;

%put _USER_;
hvempati
Obsidian | Level 7
Can you edit the format as '2018-07-02' (yy-mm--dd) in the answer. Thank you so much..!!
Tom
Super User Tom
Super User

@hvempati wrote:
Can you edit the format as '2018-07-02' (yy-mm--dd) in the answer. Thank you so much..!!

You can use the PUT() and QUOTE() functions with CALL SYMPUTX() .

call symputx('my_macro_var',quote(put(mysasdate,yymmdd10.),"'"));
hvempati
Obsidian | Level 7

Hi @Tom and @PeterClemmensen,

 

Do you think there would be a performance issue by using a method like this?

 

I was using a method where I was manually inputting all the dates. But now by using this method, I have seen the runtime for the same code was slow. (previously the code ran for 3 minutes, but now it runs for 10 minutes.) I haven't changed anything except this.

Any inputs on this?


Thank you.

 

 

Tom
Super User Tom
Super User

@hvempati wrote:

Hi @Tom and @PeterClemmensen,

 

Do you think there would be a performance issue by using a method like this?

 

I was using a method where I was manually inputting all the dates. But now by using this method, I have seen the runtime for the same code was slow. (previously the code ran for 3 minutes, but now it runs for 10 minutes.) I haven't changed anything except this.

Any inputs on this?


Thank you.

 

 


There is no difference between running code like:

select * from have where date = '2017-12-01'

And the same code generated by using a macro variable.

%let mvar='2017-12-01';
...
select * from have where date = &mvar

If you are seeing a difference then either your data has changed or your query has changed.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 1149 views
  • 3 likes
  • 3 in conversation