BookmarkSubscribeRSS Feed
StarMan
Calcite | Level 5

 

I am trying to compute the difference in dates based on the date values in two columns and store the results in a third column. The dataset I have looks like this below.

  Id      Col1_Date          Col2_Date
  1       2009-09-10         1916-02-29
  1       2009-09-10         NA

The expected output should be

  Id      Col1_Date          Col2_Date      Date_Diff
  1       2009-09-10         1916-02-29     93.6
  1       2009-09-10         NA             NA

I have tried a simple DataSet as shown below, but this is not working. I am not seeing any values in Date_Diff column.

  DATA  WORK.DF1 ;
  SET   WORK.DF1 ;
  FORMAT DOB date10.;
  Date_Diff = Col1_Date - Col2.Date;
  RUN;

Any help or suggestions on achieving this is much appreciated.

2 REPLIES 2
Reeza
Super User

Are the dates SAS dates, ie numeric with a format such as yymmdd or are they character variables?

 

If they're character you first have to convert them to a number/numeric to do calculations.

 

 


@StarMan wrote:

 

I am trying to compute the difference in dates based on the date values in two columns and store the results in a third column. The dataset I have looks like this below.

  Id      Col1_Date          Col2_Date
  1       2009-09-10         1916-02-29
  1       2009-09-10         NA

The expected output should be

  Id      Col1_Date          Col2_Date      Date_Diff
  1       2009-09-10         1916-02-29     93.6
  1       2009-09-10         NA             NA

I have tried a simple DataSet as shown below, but this is not working. I am not seeing any values in Date_Diff column.

  DATA  WORK.DF1 ;
  SET   WORK.DF1 ;
  FORMAT DOB date10.;
  Date_Diff = Col1_Date - Col2.Date;
  RUN;

Any help or suggestions on achieving this is much appreciated.


 

PaigeMiller
Diamond | Level 26

Great ... but you haven't told us what you do see so we can compare it to the expected output. We need that.

 

As a potential source of problems, your syntax is wrong here:

 

Date_Diff = Col1_Date - Col2.Date;

It should say

 

Date_Diff = Col1_Date - Col2_Date;

 

--
Paige Miller

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!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 717 views
  • 0 likes
  • 3 in conversation