SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How do we create SAS Date from Year and Week information only.?

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How do we create SAS Date from Year and Week information only.?

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

yearweek
20141
20142
20143
20144
20145
20146
20147
20148
20149
201410
201411
201412
201413
201414
201415
201416
201417
201418
201419
201420
201421
201422
201423
201424
201425
201426
201427
201428
201429
201430
201431
201432
201433
201434
201435
201436
201437
201438
201439
201440
201441
201442
201443
201444
201445
201446
201447
201448
201449
201450
201451
201452
201453

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.


Accepted Solutions
Solution
‎01-22-2015 02:50 AM
Super User
Posts: 9,681

Re: How do we create SAS Date from Year and Week information only.?

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

View solution in original post


All Replies
Super User
Posts: 6,939

Re: How do we create SAS Date from Year and Week information only.?

%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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎01-22-2015 02:50 AM
Super User
Posts: 9,681

Re: How do we create SAS Date from Year and Week information only.?

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

Super User
Posts: 3,108

Re: How do we create SAS Date from Year and Week information only.?

I suggest you check out the WEEKU, WEEKV, WEEKW informats:

http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#p0cwonojqpyr...

data _null_;

  format sasdate date7.;

  year = 2014;

  week = 1;

  sasdate = input(put(year, 4.)!! 'W' !! put(week, z2.) !! '01', weeku9.);

  put _all_;

run;

Super User
Posts: 17,836

Re: How do we create SAS Date from Year and Week information only.?

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;

Contributor
Posts: 37

Re: How do we create SAS Date from Year and Week information only.?

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.

Contributor
Posts: 37

Re: How do we create SAS Date from Year and Week information only.?

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

Contributor
Posts: 37

Re: How do we create SAS Date from Year and Week information only.?

Found here

Sample 41732

/* 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;

Super User
Posts: 9,681

Re: How do we create SAS Date from Year and Week information only.?

 
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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 1073 views
  • 11 likes
  • 5 in conversation