BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xinhui
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

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;

 

Xinhui
Obsidian | Level 7
The SAS System

Obs rdq datadate1
******************

 


The SAS System

Obs rdq datadate1
19AUG1960:14:30:2819AUG1960:14:28:51

 


The SAS System

Obs rdq datadate1
2001062820010531

 


The SAS System

The CONTENTS Procedure
Data Set NameObservationsMember TypeVariablesEngineIndexesCreatedObservation LengthLast ModifiedDeleted ObservationsProtectionCompressedData Set TypeSortedLabel Data Representation Encoding 
WORK.F_L219811
DATA2
V90
2022-01-02 18:19:42280
2022-01-02 18:19:420
 NO
 NO
  
WINDOWS_64 
euc-cn Simplified Chinese (EUC) 

 

Engine/Host Dependent InformationData Set Page SizeNumber of Data Set PagesFirst Data PageMax Obs per PageObs in First Data PageNumber of Data Set RepairsExtendObsCounterFilenameRelease CreatedHost Created
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

 

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat126
datadateNum8BEST12.BEST32.
rdqNum8BEST12.BEST32.
Patrick
Opal | Level 21

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;

Xinhui
Obsidian | Level 7

Thank you for your help. It is very detail and well explained. 

Thanks soooo much! I learned from it. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

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
  • 4 replies
  • 990 views
  • 2 likes
  • 2 in conversation