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
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.
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!
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?
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;
Can you post some sample data and the output you want?
Ksharp
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.