Calcite | Level 5

## How to calculate time differences (in minutes) between two different character variables?

Dear all,

I would like to calculate time differences between hour-of-wake-up and hours-of-eating (difference in minutes). For each participant I have the hour-of-wake up (e.g. 08:30; character variable \$5.) and the hours at which foods are consumed (e.g. 09; character variable \$3.). I've recently read a lot about handling time in SAS, however I do not (yet) succeed to calculate the difference in time. I have mainly difficulties with the difference in format statement between the two variables. Can someone please explain me how to calculate the difference?

See attachment for example dataset and the output I would like to have.

Many thanks in advance

4 REPLIES 4
Diamond | Level 26

## Re: How to calculate time differences (in minutes) between two different character variables?

Sorry, attaching things as a document is not helpful.  Post test data as a datastep in the question, follow this post if you need help:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

```data want;
time="10:15";
comp="14:";
diff= (input(substr(comp,1,2),best.)-hour(input(time,time5.))) * 60;
run;```

What your problem is is that you have two data problems.  First the complete time in text, it is always (unless for a report) to have time in a numeric time format, it will make everything far easier.  You can do this simply with input to time format (assuming full time).  The other one is more of an issue, why have a colon : if there is no time part, surely just keep it as numeric variable with number of hours.  10: makes no sense either way (unless for report).  The above takes hours away from each numeric variant and then multiply by 60 to get minutes.

To take away from this, keep data in an appropriate format.

Calcite | Level 5

## Re: How to calculate time differences (in minutes) between two different character variables?

Thank you RW9 for your advice. However, since I am a very unexperienced SAS-user it is not completely clear to me how to interpret your syntax. Do I solve both problems directly while using this syntax? In addition, it is not clear to me what to fill out at:

time="10:15";
comp="14:";

I understand that my question consists of two problems. With respect to the first problem, I've tried the following syntax:

data test_3;

set test_3;

hour2=input(hour,time5.);

wakeup2=input(wakeup,best.);

run

However, it led to the following error (see below) and no outputs were presented.

NOTE: Invalid argument to function INPUT at line 1785 column 10.

NOTE: Invalid argument to function INPUT at line 1786 column 8.

Since this syntax did not work out well, I am also not able to come up with a good solution for the second problem (calculating the difference in time).

Can you mabye clarify the syntax in more detail? And, is it necessary that the two variables have the same format before being able to distract them from each other?

Diamond | Level 26

## Re: How to calculate time differences (in minutes) between two different character variables?

Hi,

This part:

In addition, it is not clear to me what to fill out at:

time="10:15";
comp="14:";

I put that in to simulate some test data as you didn't provide any in a usable format.  You would not need it in yours as you have the data.

Now, those notes are telling you that the data you have is not in the correct format for the input statement.  Either they are not right syntactically - i.e. not HH:MM for a time5., or not actually text at all.  I suspect looking at your code here:

```data test_3;
set test_3;
hour2=input(hour,time5.);    /* hour2 sound like the hour variable */
wakeup2=input(wakeup,best.);   /* sounds like the time variable
run;

data test_3;
set test_3;
wakeup2=input(hour,time5.);
hour2=input(wakeup,best.);
run;```

You have the variables the wrong way, i.e. your putting the hour variable into time5, and the time5 into best which doesn't work.

Now two things for future consideration.  Post test data - a datastep code which will generate the test data exactly as it is on your system.  This post explains;

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

Secondly, for posting code, use the {i} or SAS run icon next to that to post code in - it retains the code formatting.

Calcite | Level 5

## Re: How to calculate time differences (in minutes) between two different character variables?

Thanks for the advice with respect to the analysis and the way of posting questions. Your suspections were correct. I had to switch the variables!

Discussion stats
• 4 replies
• 2954 views
• 0 likes
• 2 in conversation