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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

 

ballardw
Super User

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.

 

 

ConsAth
Fluorite | Level 6

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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