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...
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.