DATA Step, Macro, Functions and more

Macro programming for numeric date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Macro programming for numeric date

Hi all,

 

I have the following format for date in a table 20161031 (numeric).

 

Which is the best way to create the last 1 (20161024), 2 (20161017) weeks in the same format so as to put them in a macro element?

 

I am trying to perform some computations between the given periods but i would like to establish a more dynamic procedure for future use.

 

Thank you in advance

 

A


Accepted Solutions
Solution
‎12-12-2016 02:19 PM
Trusted Advisor
Posts: 1,356

Re: Macro programming for numeric date

If your date variable is a number like 20161031 then you need to convert it

into a sas date as in next demo

     data _null_;

       dtn = 20161031;

       date _var = input(dtn,yymmdd8.);

   run;

In case the date variable is a sas date, then just subtrcat 7 from the date to get the previous same day of week:

   data _null_;

        date_var = '31OCT2016'd;

        date_1 = date_var - 7;

        date_2 = date_var - 14;

       put date_var ddmmyy10.   date_1 ddmmyy10.   date_2 ddmmyy10. ;

 run;

 

View solution in original post


All Replies
Solution
‎12-12-2016 02:19 PM
Trusted Advisor
Posts: 1,356

Re: Macro programming for numeric date

If your date variable is a number like 20161031 then you need to convert it

into a sas date as in next demo

     data _null_;

       dtn = 20161031;

       date _var = input(dtn,yymmdd8.);

   run;

In case the date variable is a sas date, then just subtrcat 7 from the date to get the previous same day of week:

   data _null_;

        date_var = '31OCT2016'd;

        date_1 = date_var - 7;

        date_2 = date_var - 14;

       put date_var ddmmyy10.   date_1 ddmmyy10.   date_2 ddmmyy10. ;

 run;

 

Super User
Posts: 10,466

Re: Macro programming for numeric date

[ Edited ]

If you are going to do any manipulation of dates, intervals between two dates or increment dates then you want to put the values into  SAS date valued variables so the functions INTNX and INTCK are available. @Shmuel shows how. You really do not want to attempt to manipulate date values in the macro language as the syntax gets very ugly, hard to read and fragile. Use a datastep and then assign the value of the dates to macro variables using SYMPUTX.

 

 

Occasional Contributor
Posts: 11

Re: Macro programming for numeric date

Thank you very much for your replies, i usually incorporate dates in datetime into macros and using INTCK and INTNX functions but these type of date in numeric form gave me trouble because after put in a macro it wasnt beeing resolved

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 183 views
  • 0 likes
  • 3 in conversation