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

Hello!

 

I need to structure a base that compares the values day by day from last year (2019) with the equivalent days from this year (2020).

 

The problem is that these days must be equivalent not in a numerical form, but considering the weeks and weekdays.

 

For example: 01/05/2019 was the first Saturday from January, so in 2020 I need to compare it with 01/04/2019 (first Saturday from January in 2019).

 

Is there any function that calculates these dates automatically?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

@data_null__ 

@mkeintz how about 

 

NWKDOM(n, weekday, month, year) 

 

If the OP is really asking to map (say) the i'th Fri of 2019 to the i'th Fri of 2020, I don't see how NWKDOM would work, because it utilizes month as an argument.  For example the 5th Fri of 2019 is in February, but the 5th Fri of 2020 is in Jan.  it would be nice if there were a NWKDOY(n,weekday,year) function.

 

The closest I can come to is (for 365-day years) add 52 weeks as per you suggestion, and add one more week whenever the date-in-hand is the same weekday as 01JAN of that year:

 

data _null_;
  do d1='01jan2019'd to '30dec2019'd;
    d2=intnx('week',d1,52,'s');
    if weekday(d1)=weekday("01jan2019"d) then d2=d2+7;
    put (d:) (=weekdate. @40);
  end;
run;


   

The loop only goes to 30DEC2019 because 2019 has 53 Tuesdays (the last one on 31dec2019), but 2020 only has 52 Tuesdays, there is no 53rd Tuesday.

 

Of course, that doesn't take care of leap years, in which

  1. the extra week is applied to dates which are the same weekday as either 01JAN or 02JAN, and
  2. you would have to stop at 29DEC of the year in question.

 

So @Renan_Crepaldi   Which is it that you are looking for?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

7 REPLIES 7
ed_sas_member
Meteorite | Level 14

Hi @Renan_Crepaldi 

you should have a look at the following functions: Intck() and Intnx()

best,

tomrvincent
Rhodochrosite | Level 12
The intnx function will give you what you want (mostly).
data_null__
Jade | Level 19

Does this work?

 

40         data _null_;
41            do d1 = '05JAN2019'd to '05JAN2020'd by 1;
42               y = intnx('Week',d1,52,'S');
43               put (_all_)(=weekdate.);
44               end;
45            run;

d1=Saturday, January 5, 2019 y=Saturday, January 4, 2020
d1=Sunday, January 6, 2019 y=Sunday, January 5, 2020
d1=Monday, January 7, 2019 y=Monday, January 6, 2020
d1=Tuesday, January 8, 2019 y=Tuesday, January 7, 2020
d1=Wednesday, January 9, 2019 y=Wednesday, January 8, 2020
d1=Thursday, January 10, 2019 y=Thursday, January 9, 2020
d1=Friday, January 11, 2019 y=Friday, January 10, 2020
d1=Saturday, January 12, 2019 y=Saturday, January 11, 2020
d1=Sunday, January 13, 2019 y=Sunday, January 12, 2020
mkeintz
PROC Star

I am not totally clear about your question:

 


The problem is that these days must be equivalent not in a numerical form, but considering the weeks and weekdays.

 

For example: 01/05/2019 was the first Saturday from January, so in 2020 I need to compare it with 01/04/2019 (first Saturday from January in 2019).

 

Is there any function that calculates these dates automatically?

 

 

However, 1/1/2019  was the first TUESDAY of 2019, but the first Tuesday of 2020 was 1/7/2019.  That means while the first Tuesday of 2019 precedes the first Saturday of 2019, it's the opposite for 2020:  the first Saturday precedes the first Tuesday.  Is this what you want?

 

Or do you want the 52 week interval provided by @data_null__.  Just remember it would map Jan 1 to Dec 31  (or map to Dec 30 for leap years).

 

z

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
data_null__
Jade | Level 19

@mkeintz how about 

 

NWKDOM(n, weekday, month, year) 
mkeintz
PROC Star

@data_null__ 

@mkeintz how about 

 

NWKDOM(n, weekday, month, year) 

 

If the OP is really asking to map (say) the i'th Fri of 2019 to the i'th Fri of 2020, I don't see how NWKDOM would work, because it utilizes month as an argument.  For example the 5th Fri of 2019 is in February, but the 5th Fri of 2020 is in Jan.  it would be nice if there were a NWKDOY(n,weekday,year) function.

 

The closest I can come to is (for 365-day years) add 52 weeks as per you suggestion, and add one more week whenever the date-in-hand is the same weekday as 01JAN of that year:

 

data _null_;
  do d1='01jan2019'd to '30dec2019'd;
    d2=intnx('week',d1,52,'s');
    if weekday(d1)=weekday("01jan2019"d) then d2=d2+7;
    put (d:) (=weekdate. @40);
  end;
run;


   

The loop only goes to 30DEC2019 because 2019 has 53 Tuesdays (the last one on 31dec2019), but 2020 only has 52 Tuesdays, there is no 53rd Tuesday.

 

Of course, that doesn't take care of leap years, in which

  1. the extra week is applied to dates which are the same weekday as either 01JAN or 02JAN, and
  2. you would have to stop at 29DEC of the year in question.

 

So @Renan_Crepaldi   Which is it that you are looking for?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Renan_Crepaldi
Obsidian | Level 7

Wow, guys! Thanks for this many replies!

 

@mkeintz You got it right! The function I was looking for was to get the equivalent (homologous) dates for 2020 by comparing it to 2019.

 

After thinking about it a bit more, a solution I came across was to extract the week and weekday of each day on 2019, and use a second table with the same infos of 2020 plus the dates itselves. Then I made a join between these tables, bringing the equivalent date for each week/weekday of 2020.

 

I'm not sure if this way has any flaws, but I think I'll try to use the script you suggested to compare.

 

Anyway, thanks very much to everyone that helped!

 

Best regards,


Renan Crepaldi

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1026 views
  • 0 likes
  • 5 in conversation