Help using Base SAS procedures

Using calculations or sas functions?

Reply
Occasional Contributor
Posts: 14

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)

Super User
Super User
Posts: 7,988

Re: Using calculations or sas functions?

Posted in reply to mojerry_realdolmen

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

Occasional Contributor
Posts: 14

Re: Using calculations or sas functions?

true, as I have multiple calculations I do this in an empty dataset ;-)

Super User
Super User
Posts: 7,076

Re: Using calculations or sas functions?

Posted in reply to mojerry_realdolmen

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

Occasional Contributor
Posts: 14

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
Super User
Posts: 7,076

Re: Using calculations or sas functions?

Posted in reply to mojerry_realdolmen

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

Super Contributor
Posts: 266

Re: Using calculations or sas functions?

Posted in reply to mojerry_realdolmen

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

Ask a Question
Discussion stats
  • 6 replies
  • 204 views
  • 4 likes
  • 4 in conversation