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: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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