BookmarkSubscribeRSS Feed
sasbasls
Calcite | Level 5

hi,

We have some test data related to subscribers where we have service date and based on this date, we need to calculate no of years of service.

We are using current month and using a yrdif function, but it kept failing.

put(round(YRDIF(ServiceDate,current_dt,'ACT/ACT'),.01),$7.)

If service date and current date are in yyyymmdd format and if service date is greater than current_dt or in less than current_dt, please let me know how to handle so I will not get any spaces in resulting value.

thanks,

basesas9

5 REPLIES 5
art297
Opal | Level 21

A couple of questions:

1.  Are you certain that current_dt is a date or might it be a datetime?  If it is a datetime, you'll have to enclose it with the datepart() function.

2. What are you trying to output the result to?  A text file?  A SAS dataset?  Your code, at least to the right of the put statement, appears to be correct.

3. What do you want to output when Service_date is gt current date?

You will get a most useful answer if the forum has a better understanding of what you are trying to accomplish.

sasbasls
Calcite | Level 5

1. yes, it is only date in yyyymmdd format.

2. We are outputting as a character of 7 bytes.

3. if Service date is gt current date, we are making as zero.  and if Service date is blank we are defaulting date value to 17000101.

thanks!

art297
Opal | Level 21

Then some of your questions confuse me.  17000101 sure looks like a datetime value, namely July 15, 1960.

If your variables are in fact datetime variables, then you will need to use datepart for one or both variables.

You originally asked: "let me know how to handle so I will not get any spaces in resulting value".  Please explain.

And, finally, you are outputting to what?  A text file?  A SAS dataset?  A report?

FriedEgg
SAS Employee

By using the $7. format in your put statement you are corrupting your numeric values.  The outout of the put statement is always character so you should be using a proper numeric format to not obscure your result (basically you are rounding the the integer level instead of the the hundreth as you want).  To avoid the 'spaces' try using encasing your calculation in a strip().

data _null_;

do month=1,2,3;

  do day=2,3,4,7,11,13,17;

   if mod(day,2)=0 then year=2000; else year=2001;

    servicedate=mdy(month,day,year);

          currentdate=today()-day;

          yrdif=strip(put(round(yrdif(servicedate,currentdate,'ACT/ACT'),.01),best.));

          put yrdif=;

  end;

end;

run;

Ksharp
Super User

Can you post some sample data and the output you want?

Ksharp

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 782 views
  • 0 likes
  • 4 in conversation