Desktop productivity for business analysts and programmers

SAS Date variable macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

SAS Date variable macro

 

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.

 

 

 

 


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,400

Re: SAS Date variable macro

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


All Replies
PROC Star
Posts: 1,400

Re: SAS Date variable macro

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;
PROC Star
Posts: 1,400

Re: SAS Date variable macro

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

Occasional Contributor
Posts: 6

Re: SAS Date variable macro

Thank you @draycut 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.

Solution
2 weeks ago
PROC Star
Posts: 1,400

Re: SAS Date variable macro

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_;
Occasional Contributor
Posts: 6

Re: SAS Date variable macro

Can you edit the format as '2018-07-02' (yy-mm--dd) in the answer. Thank you so much..!!
Super User
Super User
Posts: 8,261

Re: SAS Date variable macro


@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.),"'"));
Occasional Contributor
Posts: 6

Re: SAS Date variable macro

[ Edited ]

Hi @Tom and @draycut,

 

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.

 

 

Super User
Super User
Posts: 8,261

Re: SAS Date variable macro


@hvempati wrote:

Hi @Tom and @draycut,

 

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.

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 173 views
  • 2 likes
  • 3 in conversation