BookmarkSubscribeRSS Feed
Elenatec
Calcite | Level 5

Hi,

I having issue by comparing the valid date to I got a dataset n ssd7bdata file format exported a small chuck of the data which looks like the attachment i. now the question I would like to have extra column with the duration in between each catogories such as:

9:21XD621Y
10:12XD621N0.51Y
10:54XD621N0.42N
11:27XD621Y0.33N

Then 4th column is the duration in between each row, so the 1st row don have value of previous value will become missing value or .

5th volumn are retain column 3rd for indication total duration of them all,  end 4th and 5th column only both rows table value are equal.

I having trouble to select previous or next row of those value and unable to use + or - to get in between time duration.

there are few more calculation need to be done later if these steps are completed.

any idea? and thanks first.

Regards,

5 REPLIES 5
UrvishShah
Fluorite | Level 6

Hi,

Your data seems like you want to calculate difference between time for each category...But the way you presanted your query, i am not able understand what excatly you want to do...

Like you wrote 4th and 5th column, but in your sample data there is only 3 columns...So it will be good if you convey your query in meaningful way so that we can solve your query...

Though, you simply need difference b/w time then please have look at following code...

data want;

  set have;

  checkin_time = dif(time);

run;

Then you can calaulate more based on the value of CHECKIN_TIME variable...

-Urvish

sascom10
Calcite | Level 5

Also, try exploring lag() function which might help u you getting there.

Gud Luk!

jwsquillace
SAS Employee

Here is sample code that calculcates time difference and previous checkin values.  You didn't say about across different categories, so I guessed and reset the previous values to missing.

The test_table appears to be tab-delimited, so I allowed for that, too.

The key is to write the observation before assigning the current values to the previous value variables.


data test;
infile 'c:\temp\test_table.txt'  truncover firstobs=2 dlm='09'x;
input Time :time5. Categories :$5. Checkin :$1. ;
length prev_category $5;
retain prev_checkin ' ' prev_time . prev_category ' ';

if categories ne prev_category then do;    /* start of new category? */
  prev_time = .;
  prev_checkin = ' ';
end;
timediff = time - prev_time;           /* calculate difference */
output;                                /* write observation */
prev_checkin = checkin;                /* save values for comparison with next row */
prev_time = time;
prev_category = categories;
format time prev_time timediff time5.;
keep time categories checkin prev_checkin timediff;
run;

proc print data=test; run;

Jan

Elenatec
Calcite | Level 5

thanks guys,

I used lag() fucntion which work fine at the momeny, I still having some issue from the input with alot matchmaking to get the end result, just a quick question about the datasets if the categories always random generated from SAS do I need to sort before create a new datasets because I wanted to calculate the time duration in between each rows with the same categories, does the retain & lag function work the same?

Regards,

jwsquillace
SAS Employee

The  LAG function works from neighboring observations, so you want to have all the observations for a category together in the input data set.

If the data set is very large, you may find it more efficient to use PROC DATASETS, MODIFY and INDEX CREATE to avoid having to physically rearrange the observations.

Cheers,

Jan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1081 views
  • 6 likes
  • 4 in conversation