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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 890 views
  • 4 likes
  • 4 in conversation