Hi Experts,
I have two variables "Year" and "Week" using these I need to create a SAS date. I know there is no standalone SAS function available which can do this task but using few functions in conjunction may be useful and tried few but failed. Here is my data
year | week |
2014 | 1 |
2014 | 2 |
2014 | 3 |
2014 | 4 |
2014 | 5 |
2014 | 6 |
2014 | 7 |
2014 | 8 |
2014 | 9 |
2014 | 10 |
2014 | 11 |
2014 | 12 |
2014 | 13 |
2014 | 14 |
2014 | 15 |
2014 | 16 |
2014 | 17 |
2014 | 18 |
2014 | 19 |
2014 | 20 |
2014 | 21 |
2014 | 22 |
2014 | 23 |
2014 | 24 |
2014 | 25 |
2014 | 26 |
2014 | 27 |
2014 | 28 |
2014 | 29 |
2014 | 30 |
2014 | 31 |
2014 | 32 |
2014 | 33 |
2014 | 34 |
2014 | 35 |
2014 | 36 |
2014 | 37 |
2014 | 38 |
2014 | 39 |
2014 | 40 |
2014 | 41 |
2014 | 42 |
2014 | 43 |
2014 | 44 |
2014 | 45 |
2014 | 46 |
2014 | 47 |
2014 | 48 |
2014 | 49 |
2014 | 50 |
2014 | 51 |
2014 | 52 |
2014 | 53 |
And as we know week 1 in year 2014 ended on 4th - Jan- Saturday (SAS date Sunday=1 and Saturday=7). Similarly week 2 ended on 11th -January- Saturday and so on which will give me last value in dataset as 3rd-Jan-2015- Saturday(any format is OK).
So to achieve this given the information and we have liberty to hard code 1st value as "Saturday 4 Jan 2014" if at all we need to use it for future reference in the date creation.
Any thoughts.
Thanks.
Harshad M.
You can use WEEK as interval and option E of intnx() to get weekend.
data have; input year week ; date=intnx('week',mdy(1,1,year),week-1,'e'); format date date9.; cards; 2014 1 2014 2 2014 3 2014 4 2014 5 2014 6 2014 7 2014 8 2014 9 2014 10 2014 11 2014 12 2014 13 2014 14 2014 15 2014 16 2014 17 2014 18 2014 19 2014 20 2014 21 2014 22 2014 23 2014 24 2014 25 2014 26 2014 27 2014 28 2014 29 2014 30 2014 31 2014 32 2014 33 2014 34 2014 35 2014 36 2014 37 2014 38 2014 39 2014 40 2014 41 2014 42 2014 43 2014 44 2014 45 2014 46 2014 47 2014 48 2014 49 2014 50 2014 51 2014 52 2014 53 ; run;
Xia Keshan
%let year=2014;
data want (keep=year week date);
format
year 4.
week 2.
date date9.
;
year = &year;
* find 1st Saturday in year;
date = "01jan&year"d;
do until (weekday(date) = 7);
date = date + 1;
end;
if week(date) = 0 then date = date + 7;
do until (year(date) > year);
week = week(date);
output;
date = date + 7;
end;
run;
You can use WEEK as interval and option E of intnx() to get weekend.
data have; input year week ; date=intnx('week',mdy(1,1,year),week-1,'e'); format date date9.; cards; 2014 1 2014 2 2014 3 2014 4 2014 5 2014 6 2014 7 2014 8 2014 9 2014 10 2014 11 2014 12 2014 13 2014 14 2014 15 2014 16 2014 17 2014 18 2014 19 2014 20 2014 21 2014 22 2014 23 2014 24 2014 25 2014 26 2014 27 2014 28 2014 29 2014 30 2014 31 2014 32 2014 33 2014 34 2014 35 2014 36 2014 37 2014 38 2014 39 2014 40 2014 41 2014 42 2014 43 2014 44 2014 45 2014 46 2014 47 2014 48 2014 49 2014 50 2014 51 2014 52 2014 53 ; run;
Xia Keshan
I suggest you check out the WEEKU, WEEKV, WEEKW informats:
data _null_;
format sasdate date7.;
year = 2014;
week = 1;
sasdate = input(put(year, 4.)!! 'W' !! put(week, z2.) !! '01', weeku9.);
put _all_;
run;
It all depends on what you want to use it for in the future. You can create a character variable and use it with input and weeku/v/w informat.
This might not quite meet your needs, mostly because I haven't tailored the days/u/vw options.
data example;
year=2014;
do i=1 to 52;
text=catx("-", year, "W"||put(i, z2.), "01");
sas_date=input(text, weekw9.);
output;
end;
format sas_date date9.;
run;
When I was trying to do it myself I used INTNX and MDY but I was not aware that we can use "WEEK-1"(variable in conjunction with mathematical operation) as parameter in INTNX.
Thanks for answers.
From output of above solution how can we create Week of Month variable which will take only 5 values ('1','2','3','4','5') representing the first to fifth week of that month.
This way week 5 of our dataset will be week 1 of February and WOM(week of month) variable should show "1" and respectively "2","3","4" for 6,7 and 8.
Same way for March
(WEEK) (WOM-new var)
9 1
10 2
11 3
12 4
13 5
Thanks.
Harshad
Found here
/* Sample data */
data ds1;
do date='01nov2010'd to '31dec2010'd;
output;
end;
run;
/* The WEEK variable returns a value of 1 to 5 based on the number of weeks in a month
using a combination of the INTNX and INTCK functions. */
data ds2;
set ds1;
week=intck('week',intnx('month',date,0),date)+1;
run;
proc print;
format date date9.;
run;
data have; input year week ; date=intnx('week',mdy(1,1,year),week-1,'e'); if month(date) ne month(lag(date)) then wom=0; wom+1; format date date9.; cards; 2014 1 2014 2 2014 3 2014 4 2014 5 2014 6 2014 7 2014 8 2014 9 2014 10 2014 11 2014 12 2014 13 2014 14 2014 15 2014 16 2014 17 2014 18 2014 19 2014 20 2014 21 2014 22 2014 23 2014 24 2014 25 2014 26 2014 27 2014 28 2014 29 2014 30 2014 31 2014 32 2014 33 2014 34 2014 35 2014 36 2014 37 2014 38 2014 39 2014 40 2014 41 2014 42 2014 43 2014 44 2014 45 2014 46 2014 47 2014 48 2014 49 2014 50 2014 51 2014 52 2014 53 ; run;
Xia Keshan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.