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

Is there a SAS function which returns the month based on the week identifier?

 

Ex.: Week 26 => 06

or    Week 26 => JUNE

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @PierreYvesILY,

 

You could pick one of the WEEK... informats and use the MONTH, INPUT, CATS and PUT functions to compute the month from week number and year.

 

Example:

data test;
do week=26 to 27;
  do year=2019 to 2021;
    mon=month(input(cats(year,'W',put(week,z2.)),weekv.));
    output;
  end;
end;
run;

Result:

week    year    mon

 26     2019     6
 26     2020     6
 26     2021     6
 27     2019     7
 27     2020     6
 27     2021     7

In this example the month is (by default) calculated for the Monday of the respective week.

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

Not that I know of.

Since a week can be split between two months, you need to figure how you want to map this.

So I think that you need to go via a date first.

Data never sleeps
Patrick
Opal | Level 21

I believe what you're asking for is not possible without also knowing the year AND the logic used to determine the first day of week 1.

 

For below found here: https://www.epochconverter.com/weeks/2020 

Patrick_0-1625214474568.pngPatrick_1-1625214621690.png

 

Without a year what month should week 1 get - and what month week 52 or 53?

FreelanceReinh
Jade | Level 19

Hello @PierreYvesILY,

 

You could pick one of the WEEK... informats and use the MONTH, INPUT, CATS and PUT functions to compute the month from week number and year.

 

Example:

data test;
do week=26 to 27;
  do year=2019 to 2021;
    mon=month(input(cats(year,'W',put(week,z2.)),weekv.));
    output;
  end;
end;
run;

Result:

week    year    mon

 26     2019     6
 26     2020     6
 26     2021     6
 27     2019     7
 27     2020     6
 27     2021     7

In this example the month is (by default) calculated for the Monday of the respective week.

PierreYvesILY
Pyrite | Level 9

hey Reinhard,

 

thanks a lot for this very good solution, which is exactly what I was looking for! 

I just modified my program with it and tested it, it's just perfect.

 

Have a nice WE

Regards, 

PY