BookmarkSubscribeRSS Feed
mojerry_realdolmen
Obsidian | Level 7

Hi,

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)

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

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.

day=day(input(&MVAR.,yymmdd10.));

mojerry_realdolmen
Obsidian | Level 7

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

Tom
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.);

mojerry_realdolmen
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.

Tom
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.);

Aman4SAS
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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1256 views
  • 4 likes
  • 4 in conversation