Obsidian | Level 7

## Using calculations or sas functions?

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
Diamond | Level 26

## Re: Using calculations or sas functions?

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

Obsidian | Level 7

## Re: Using calculations or sas functions?

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

Super User

## Re: Using calculations or sas functions?

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

## Re: Using calculations or sas functions?

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

## Re: Using calculations or sas functions?

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

## Re: Using calculations or sas functions?

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

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