Hi all,
This might be a simple question: I have week and year information, how can I get the corresponding month?
Thanks a lot!
Lizi
If you want a date, it being the first date in the given week then you can do something like:
data have;
year='2013'; week=1; output;
year='2013'; week=2; output;
year='2013'; week=3; output;
run;
data want;
set have;
if week=1 then new_date=input("01JAN"||strip(year),date9.);
else new_date=input("01JAN"||strip(year),date9.) + ((week-1) * 7);
format new_date date9.;
run;
Rembering that a date variable is the number of days since 01Jan1960, so work out the year as 01JAN, gives you the number for that day, then add the week number less 1 (as we want start) multiplied by 7.
Can you give a few examples of values you have? Are they like 201501 for first week of 2015? 201552 for fifty-second week of 2015? Or some other format? Also, might need to define what day is the start of the week, etc.
Thanks, Quentin.
It was basically two columns:
year week
2013 1
2013 2
...
2013 52
2014 1
2014 2
...
2014 52.
data Want;
input Year week;
Mnt_Name=put(input(cat(0101,Year),ddmmyy10.)+(Week-1)*7,monname3.);
Mnt_Num=Month(input(cat(0101,Year),ddmmyy10.)+(Week-1)*7);
cards;
2013 1
2013 2
2013 16
2013 52
2014 1
2014 2
2014 52
;
proc print;run;
If you want a date, it being the first date in the given week then you can do something like:
data have;
year='2013'; week=1; output;
year='2013'; week=2; output;
year='2013'; week=3; output;
run;
data want;
set have;
if week=1 then new_date=input("01JAN"||strip(year),date9.);
else new_date=input("01JAN"||strip(year),date9.) + ((week-1) * 7);
format new_date date9.;
run;
Rembering that a date variable is the number of days since 01Jan1960, so work out the year as 01JAN, gives you the number for that day, then add the week number less 1 (as we want start) multiplied by 7.
What would 2015 week 1 mean? Jan 1, 2015 was a Thursday. So what is the corresponding month for week 1, January?
What would be the first week number of 2015 that would map to February? What's the rule that says which month a year-week number should map to?
Hi Liziwu,
You must specify what do you need, a desired output maybe.
What do you need, the month at the begining of the week, at the end, the week is in the month if there are 4 or more days on it?
data Want;
input Year week;
month=month(intnx('week',mdy(1,1,year),week));;
cards;
2013 1
2013 2
2013 16
2013 52
2014 1
2014 2
2014 52
;
Check informat WEEKU. WEEKV. WEEKW.
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!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.