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

I am trying to find the starting date of the week of a date value.

For example, 2021-04-30 will return 2021-04-26(1st date of the week).

i am using the code:

start_week = intnx('week',date,0,'b');

format start_week date10.;

But this is returning null value.

Can anyone help me.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
You can have a character variable appear that way though. Post the log from your code that will help us determine what the issue is.

Or try this and let us know what happens:

week = intnx('week', input(date, yymmdd10.),0,'b');

View solution in original post

11 REPLIES 11
anandmgjsa
Fluorite | Level 6

I am trying to find the starting date of the week of a date value.

For example, 2021-04-30 will return 2021-04-26(1st date of the week).

i am using the code:

start_week = intnx('week',date,0,'b');

format start_week date10.;

But this is returning null value.

Can anyone help me.

PeterClemmensen
Tourmaline | Level 20

Your code seems fine. I think it is the input value of date.

 

See the code below.

 

data _null_;
   start_week = intnx('week', today(), 0, 'b');
   format start_week date10.;
   put start_week=;
run;
ballardw
Super User

If date is missing the result will be missing.

If date is character you should see "invalid data" such as this:

104  data junk;
105     date ='2021-01-01';
106     week = intnx('week', date,1,'b');
107  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      106:25
NOTE: Invalid numeric data, date='2021-01-01' , at line 106 column 25.
date=2021-01-01 week=. _ERROR_=1 _N_=1

If date is actually a datetime value you should get an invalid message like this because the values of datetimes when used as a date without appropriate conversion or options often exceed the range of values the date related functions will handle.

108  data junk;
109     date ='01JAN2021:12:15:00'dt;
110     put date= datetime18.;
111     week = intnx('week', date,1,'b');
112  run;

date=01JAN21:12:15:00
NOTE: Invalid argument to function INTNX('week',1925122500,1,'b') at line 111 column 11.
date=1925122500 week=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the
      operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 111:11

All of these cases will return missing values from the intnx function.

So, which values are you actually supplying to the function? And are you getting notes in the log?

anandmgjsa
Fluorite | Level 6
Yes, I am getting the notes mentioned by you. But the input used by me is date itself not datetime. Or I am missing something. Please clarify.
Reeza
Super User
What is the type and format of the date variable?
It should be a numeric variable with a format of yymmdd10.
anandmgjsa
Fluorite | Level 6
Yes, it is in the same format yymmdd10. Example:2020-05-28.
Reeza
Super User
You can have a character variable appear that way though. Post the log from your code that will help us determine what the issue is.

Or try this and let us know what happens:

week = intnx('week', input(date, yymmdd10.),0,'b');
anandmgjsa
Fluorite | Level 6

Yes, it worked. Thanks a lot!!

Reeza
Super User
That means your variable was a character variable, not a SAS date.
ballardw
Super User

@anandmgjsa wrote:
Yes, I am getting the notes mentioned by you. But the input used by me is date itself not datetime. Or I am missing something. Please clarify.

That means that you really have to follow @Kurt_Bremser's recommendation and show us your log. Copy the text for the entire data step plus messages from the log. On the forum open a text box by clicking on the </> icon above the message window and paste.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 11 replies
  • 1132 views
  • 1 like
  • 5 in conversation