Hi all,
I was just wondering how I am able to find the week of the variable date.
the variable shows the dates in YYMMDD format (2015-08-20), however the proc contents shows the variable ac character $14.
So the week of 2015-08-20 is 34. The dataset is too big and I am not able to work with Excel. I should use SAS function.
You help will be appreciated.
Thanks
Convert to a real SAS date value, and use the week() function:
data _null_;
chardate = '2015-08-20';
numdate = input(chardate,yymmdd10.);
format numdate yymmddd10.;
weeknum = week(numdate);
put _all_;
run;
Log:
24 data _null_;
25 chardate = '2015-08-20';
26 numdate = input(chardate,yymmdd10.);
27 format numdate yymmddd10.;
28 weeknum = week(numdate);
29 put _all_;
30 run;
chardate=2015-08-20 numdate=2015-08-20 weeknum=33 _ERROR_=0 _N_=1
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Convert to a real SAS date value, and use the week() function:
data _null_;
chardate = '2015-08-20';
numdate = input(chardate,yymmdd10.);
format numdate yymmddd10.;
weeknum = week(numdate);
put _all_;
run;
Log:
24 data _null_;
25 chardate = '2015-08-20';
26 numdate = input(chardate,yymmdd10.);
27 format numdate yymmddd10.;
28 weeknum = week(numdate);
29 put _all_;
30 run;
chardate=2015-08-20 numdate=2015-08-20 weeknum=33 _ERROR_=0 _N_=1
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
If your variable containing the date is of character type, you must convert it to a numeric format first.
Input converts your character variable read in the format yymmdd10.
data want;
date_char_fmt='2015-08-20';
week_of_date=week(input(date_char_fmt,yymmdd10.));
run;
There are different definitions for deriving the week of the year. These are explained in the documentation: https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n1ka2ulrvrjlasn0z7beco2yrgas.htm...
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.