BookmarkSubscribeRSS Feed
DeniseDorland
Calcite | Level 5

I want to add 24 hrs to DTC = 2023-10-16T09:30 so as to get the result as 2023-10-17T09:30. How should I write the SAS code?

10 REPLIES 10
SASKiwi
PROC Star

Try this:

data Want;
  set Have;
  MyDateTime = MyDateTime + '24:00't;
run;
DeniseDorland
Calcite | Level 5

DeniseDorland_0-1703285915674.png

getting this error

Tom
Super User Tom
Super User

You cannot add character strings.

You will have to first convert the character string into an actual datetime value.

input(dct_trt_1,anydtdtm20.) + '24:00:00't 

Do you want get a datetime value back?  or do you want another character string?

DeniseDorland
Calcite | Level 5
Answer: 2023-10-17T09:30
Tom
Super User Tom
Super User

@DeniseDorland wrote:
Answer: 2023-10-17T09:30

Huh?

Do you want a character string? Or a number?

 

Do you plan on doing more arithmetic with this new datetime value? If so keep it as a number, otherwise you will have to convert it back from character again.

Tom
Super User Tom
Super User

Since both DATETIME and TIME values are stored in seconds the easiest way is to add 24 hours worth of seconds, or '24:00:00't .

data want;
  set have;
  new_var = old_var + '24:00:00't ;
  format new_var datetime19.;
run;
DeniseDorland
Calcite | Level 5

DeniseDorland_0-1703286238020.png

still getting the error

ballardw
Super User

Your DTC_TRT_1 variablef, and therefore like the others, are CHARACTER values. They are character values that will not convert to a "nice" numeric datetime value unless you force such. When you use a character variable in arithmetic SAS will attempt to convert it to a number but has very limited rules when the value is more complex than something like "123". So it is up to you to do several things: 1) convert the string to a datetime value to use in the addition.

If the result is a new numeric  variable then you will want to assign a FORMAT to display the value in a manner that people can recognize.

 

data example;
   dtc_trt_1 ="2023-10-16T09:30";
   dtc_wsh =    input(dtc_trt_1,e8601dt.) +'24:00't;
   format dtc_wsh e8601dt.;
run;

Note that I create a variable that is character as your LOG states for dtc_trt_1. Then use the INPUT function with an appropriate INFORMAT to create a datetime numeric value then add the time literal 24 hours. The shortest default format SAS has supplied that matches your appearance will have 19 characters so will display seconds as :00. If that is objectionable then it is possible to create a custom format that does otherwise.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates, times and datetime values.

 

Please do yourself an us a favor and copy the text from log and on the forum open a text box using the </> icon above the message window and paste the text. That way we can copy/paste/edit your text. If we have to retype considerable amounts of text to answer questions you may not get as good or complete an answer.

Tom
Super User Tom
Super User

If you just want to make another string it might be easier to just convert the beginning of your string to a DATE value and then subtract one before converting it back into a string.

dtc_wsh = put(input(dtc_trt_1,yymmdd10.)-1,yymmdd10.)||substr(ctc_trt_1,11,5);

Or perhaps simpler to copy the string first (then you get a variable with the same length as the original) and then replace the first 10 characters with the new date.

dtc_wsh = dtc_trt_1;
substr(dtc_wsh,1,10) =put(input(dtc_trt_1,yymmdd10.)-1,yymmdd10.);

 

SASKiwi
PROC Star

@DeniseDorland - I'm curious to know why your datetime is stored in a character variable - is that by design or just the way it was imported into SAS? As you can see, doing calculations on a character datetime is a whole lot harder than if it was a SAS datatime which is stored in a numeric variable.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 10 replies
  • 1219 views
  • 0 likes
  • 4 in conversation