Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates and how to manipulate them

Status
Not open for further replies.

evaaseow

Programmer
Jan 25, 2007
29
CA
Ok, so I come from a VB/VBA background and am wondering how to manipulate dates.

For example, if I wanted the week of the current month, I would write format(now(),"WW") in vba. Is there similar functionalities in SAS?
 
There's a selection of date functions that you can use to extract certain bits of information. I'd recommend havign a browse of the doco:-
Look under
Base SAS > SAS Language Reference: Dictionary > Dictionaru of Language Elements > Functions and Call Routines > Functions and Call Routines by Category
Then scroll down to Date and Time, they're all listed there. I'm not sure if there's a specific "week of month" function, but you should be able to get it using the INTNX function to find the first of the month, then the INTCK function to get the number of weeks between the 1st of the month and the date you are checking... read the notes for these 2 functions carefully as they may not do exactly what you expect them to do, as they count only the first of the week or the first of the month.
Shout back if you have trouble working out the logic.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thank you for pointing me to the Online Doc. One question, is it possible to retrieve the week of the month and not year?
 
Ok, I have a program that is automatically run every wednesday. There is a cycle field that needs to be incremented everytime it runs. The value is based on the week # in the month. I have to also take into consideration that there are some months with 5 weeks rather than the regular 4.

Can anyone help me please?
 
For the week of the month, I would say you'd need to use intnx to get teh first day of the month, then count the number of weeks between now and the first of the month using INTCK.
Some variant of the following (run this, see what it does, the results get dropped into the log, you can then play with the code as necessary to get the correct results, but I think this might be it):-
Code:
data _null_;
   format month1 ddmmyy10.;
   month1 = intnx('MONTH',intnx('MONTH',today(),-1),+1);
   weeks_since = intck('WEEK',month1,today());

   put month1=;
   put weeks_since=;
run;

You'll notice that I used INTNX to go back 1 months, then forward 1 month, this is because INTNX('MONTH',today(),-1) goes back to the 1st of the previous month (ie today that'll be the 1st of December 2006), so I need to then go forwards 1 months again to get the 1st of the current month. The INTCK line there basically counts the number of MONDAYS between the two dates.
TODAY() returns the current date.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top