Hi,
I have two variables :rdq and datadate,
I wanna calculate the days between two dates,
I use the folllowing code:
data f_f;
set f_l2;
days=intck('day', datadate, rdq);
run;
but the code not work all the days are '.'.
Could you please help me correct the code?
Thanks in advance.
This made it clear. You have numerical variables with values that express a date as yyyymmdd but that are not a SAS date value.
We need to convert these numbers to a SAS date value in order to do something useful with it.
data f_l2;
datadate=20010628;
rdq=20010531;
run;
data test;
set f_l2;
/* convert number in form yyyymmdd to SAS Date value (count of days since 1/1/1960) */
datadate_SASDate =input(put(datadate,8.),yymmdd8.);
rdq_SASDate =input(put(rdq,8.),yymmdd8.);
/* apply format to SAS Date values so they print as human readable dates and not just the count of days */
/* a format does not change the internal value but just how the values get printed */
format datadate_SASDate rdq_SASDate date9.;
/* calculation options */
days_1=intck('day', datadate_SASDate, rdq_SASDate);
days_2=intck('day', input(put(datadate,8.),yymmdd8.), input(put(rdq,8.),yymmdd8.));
/* and because a SAS Date value is just a count of days we now can use a simple substraction */
days_1b=rdq_SASDate-datadate_SASDate;
days_2b=input(put(rdq,8.),yymmdd8.) - input(put(datadate,8.),yymmdd8.);
run;
Your code as such looks valid so the issue must be in the data.
Your two variables datadate and rdq must contain SAS Date values for your code to work.
A SAS Date value is the count of days since 1/1/1960.
It's not really possible to say much more without access to your data. If you can attach your SAS table (sas7bdat) file with a few rows to your question. You could also execute below code and post the result here. That would also tell us more about your real data.
proc print data=f_l2(obs=1 keep=datadate rdq);
format datadate rdq date9.;
run;
proc print data=f_l2(obs=1 keep=datadate rdq);
format datadate rdq datetime21.;
run;
proc print data=f_l2(obs=1 keep=datadate rdq);
format datadate rdq 32.;
run;
proc contents data=f_l2(keep=datadate rdq);
run;quit;
The SAS System |
********* | ********* |
The SAS System |
19AUG1960:14:30:28 | 19AUG1960:14:28:51 |
The SAS System |
20010628 | 20010531 |
The SAS System |
WORK.F_L2 | 19811 |
DATA | 2 |
V9 | 0 |
2022-01-02 18:19:42 | 280 |
2022-01-02 18:19:42 | 0 |
NO | |
NO | |
WINDOWS_64 | |
euc-cn Simplified Chinese (EUC) |
65536 |
86 |
1 |
233 |
217 |
0 |
YES |
C:\Users\lenovo\AppData\Local\Temp\SAS Temporary Files\_TD16424_DESKTOP-U6HBCL9_\f_l2.sas7bdat |
9.0401M2 |
X64_8HOME |
datadate | Num | 8 | BEST12. | BEST32. |
rdq | Num | 8 | BEST12. | BEST32. |
This made it clear. You have numerical variables with values that express a date as yyyymmdd but that are not a SAS date value.
We need to convert these numbers to a SAS date value in order to do something useful with it.
data f_l2;
datadate=20010628;
rdq=20010531;
run;
data test;
set f_l2;
/* convert number in form yyyymmdd to SAS Date value (count of days since 1/1/1960) */
datadate_SASDate =input(put(datadate,8.),yymmdd8.);
rdq_SASDate =input(put(rdq,8.),yymmdd8.);
/* apply format to SAS Date values so they print as human readable dates and not just the count of days */
/* a format does not change the internal value but just how the values get printed */
format datadate_SASDate rdq_SASDate date9.;
/* calculation options */
days_1=intck('day', datadate_SASDate, rdq_SASDate);
days_2=intck('day', input(put(datadate,8.),yymmdd8.), input(put(rdq,8.),yymmdd8.));
/* and because a SAS Date value is just a count of days we now can use a simple substraction */
days_1b=rdq_SASDate-datadate_SASDate;
days_2b=input(put(rdq,8.),yymmdd8.) - input(put(datadate,8.),yymmdd8.);
run;
Thank you for your help. It is very detail and well explained.
Thanks soooo much! I learned from it.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.