BookmarkSubscribeRSS Feed
gzr2mz39
Quartz | Level 8

Typically cupa_dt is greater than start_dt. However, when start_dt is greater than cupa_dt I need to switch around start_dt and cupa_dt to get the correct difference between the two dates. Is there any way to accomplish this without creating a flag (ie cc_check)?

cupa_dt=dhms(TDT,0,0,TTM);

start_dt=dhms(CSDAT,0,0,CSTIM);

cc_check=0;

if (start_dt>cupa_dt and cupa_dt ne .) then cc_check=1;

if(cc_check=0) then do;
cc_diff=round(((cupa_dt-start_dt)/86400),0.01);
end;

if(cc_check=1) then do;
cc_diff=round(((start_dt-cupa_dt)/86400),0.01);
end;

Thank you.

2 REPLIES 2
stat_sas
Ammonite | Level 13

Just add abs function


cc_diff=round(((abs(cupa_dt-start_dt))/86400),0.01);

Peter_C
Rhodochrosite | Level 12

although you have one solution, here is another

difference = range( date1, date2 ) ;

Here is some code to demo the result .

data ;

do date1 =  today() - 3 to today()+2, . ;

do date2 =  today() - 3 to today()+2, . ;

  diff= range( date1,date2) ;

  output ;

end; end;

stop;

format date: date. ;

run ;

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 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
  • 2 replies
  • 805 views
  • 0 likes
  • 3 in conversation