SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
leeshea06
Calcite | Level 5

Someone passed me date variables like that follows.  Both Finished_date (Char 15 $15. $15.) and Start_date (Char 10 ) are characters. 

 I'd like to compute difference in days between Finished_date  and Start_date; that is, Diff_days = Finished_date - Start_date, but it does not work. How can I do this simple subtraction? convert character to date format first? 

Thank you very much for your help! Lee


Finished_date Start_date
2/15/2020 10/15/2018
2/15/2020 3/1/2018
2/15/2020 7/8/2018
2/16/2020 9:28 8/20/2019
2/16/2020 12:44 1/1/2017
2/17/2020 0:10 9/25/2016
2/17/2020 7:54 10/15/2019

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

I would play it safe and go with :

 

diff_days = input(scan(finished_date, 1, " "), mmddyy10.) - input(scan(start_date, 1, " "), mmddyy10.);

PG

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Given that both are character variables, you have to convert from character to numeric on the fly, as part of the computation:

diff_days = input(finished_date, mmddyy10.) - input(start_date, mmddyy10.);

This assumes that the time values are part of FINISHED_DATE, not part of START_DATE.  Even then, there could be complications if you have 8-character dates with a time piece like 2/6/2017 9:28.  If that occurs, a slightly more complex formula would be needed.

PGStats
Opal | Level 21

I would play it safe and go with :

 

diff_days = input(scan(finished_date, 1, " "), mmddyy10.) - input(scan(start_date, 1, " "), mmddyy10.);

PG
leeshea06
Calcite | Level 5
works, thanks! Lee

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 5502 views
  • 0 likes
  • 3 in conversation