BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fama
Fluorite | Level 6

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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
sustagens
Pyrite | Level 9

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;
SASKiwi
PROC Star

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 861 views
  • 0 likes
  • 4 in conversation