BookmarkSubscribeRSS Feed
Obsidian | Level 7


I've got a technical question. What the best to use?

to know the number of days between two dates should I use datedif or just do the math? lastdate-firstdate gives also the number of days.

b=put(year(date),z4.)!!put(month(date),z2.)  or b=put(date,yymmn6.)

working on dates where I have is stored in two macrovariables. The first one is numeric and the second one is a format (yyyy/mm/dd). If I want to know the day should I use the day(numdate) or do substr(date_char,9,2)

Diamond | Level 26 RW9
Diamond | Level 26


On the datediff, if its just number of days between you want, then - would probably be cheaper although you will hardly see any difference unless you have thousands or rows.

Macro variables are always text, so to do any calculation you need to input them into numeric.


Obsidian | Level 7

true, as I have multiple calculations I do this in an empty dataset 😉

Super User Tom
Super User

Whichever method will be easier to maintain and explain.

%let numdt=19912 ;

%let chardt=2014/07/08 ;

%put day=%scan(&chardt,-1,/);

%put day=%substr(&chardt,9);

%put day=%sysfunc(day(%sysfunc(inputn(&chardt,yymmdd10.))));

%put day=%sysfunc(day(&numdt));

%put day=%sysfunc(day(%sysfunc(inputn(&chardt,yymmdd10.))),z2.);

%put day=%sysfunc(day(&numdt),z2.);

Obsidian | Level 7

to me the last code is the best looking. When you read it you know directly what it's doing.

the previous line is if your data is formatted but works as espected.

Super User Tom
Super User

If you make the character dates use DATE format instead of YYMMDD then you can use them as date literals.

%let numdt=19912 ;

%let chardt=08JUL2014 ;

%put day=%sysfunc(day(&numdt),z2.);

%put day=%sysfunc(day("&chardt"d),z2.);

Obsidian | Level 7

%let x=19912 ;

%let y=2014/10/10;

%let z = %sysfunc(inputn(&y,yymmdd10.));

%let a= %sysfunc(intck(day,&x,&z));

%put &x &y &z &a;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 4 in conversation