BookmarkSubscribeRSS Feed
Andalusia
Obsidian | Level 7

Hello SAS Experts,

I have 2 sets:

SET-1

IDDateTime
100101NOV202010:01:01
100201NOV202016:30:10
100301NOV202021:45:45


SET-2

IDTimePrice
10010.000125.22
10010.500122.23
10011.000114.26
10011.500130.60
10020.000899.33
10020.500881.23
10021.0001000.01
10021.5001035.23
10030.000667.22
10030.500668.33
10031.000669.30
10031.500678.53
10032.000710.22

 

The target of this all is to join these two sets together and to concatenate date + time from SET1 and time from SET2. So It will be [Date+time+time]. The time of SET1 is HMS and this is actually the start time of a trend. The time is of SET-2 is in seconds/milliseconds and this is actually the time in seconds/milliseconds during the trend. Every new trend starts at 0.00 which is actually equal to the start  time of a trend. Each trend takes about 1,5 or 2 seconds.

This is what I expect it to be:
SET-3

IDDateTimePrice
100111/01/2020 10:01:01:000125.22
100111/01/2020 10:01:01:500122.23
100111/01/2020 10:01:02:000114.26
100111/01/2020 10:01:02:500130.60
100211/01/2020 16:30:10:000899.33
100211/01/2020 16:30:10:500881.23
100211/01/2020 16:30:11:0001000.01
100211/01/2020 16:30:11:5001035.23
100311/01/2020 21:45:45:000667.22
100311/01/2020 21:45:45:500668.33
100311/01/2020 21:45:46:000669.30
100311/01/2020 21:45:46:500678.53
100311/01/2020 21:45:47:000710.22

 

Please note that ''deleting'' the millisecond is not an option, because the price fluctuates twice every second. I really need it to be DATE-HOUR-MINUT-SECOND-MILLISECOND. Also please note that the real datasets are of course much bigger.

18 REPLIES 18
ballardw
Super User

Divide the time in Set2 so that the units are seconds instead of milliseconds before combining with the values in Set1, which would be done by either adding the fractional second or use of the intnx function

 

You make a date time valued variable from a date and time with

 

dt = dhms(datevar,0,0,timevar);

Andalusia
Obsidian | Level 7
@ballardw that's not an option. In SET3 I really need it to be DATE-HOUR-MINUTE-SECOND-MILLISECOND. I don't want to loose my milliseconds.
ballardw
Super User

 


@Andalusia wrote:
@ballardw that's not an option. In SET3 I really need it to be DATE-HOUR-MINUTE-SECOND-MILLISECOND. I don't want to loose my milliseconds.

A millisecond is one-onethousandth of a second, a decimal portion of a second. That is how SAS uses times values. That does NOT lose any milliseconds. You may need to assign a different format to see the decimal portion which properly looks like

 

11/01/2020 10:01:01.500  (for 500 milliseconds or half a second). If this is supposed to be 5 milliseconds then

11/01/2020 10:01:01.005

 

Otherwise you will be in the world of Character values and impossible to do almost anything.

PaigeMiller
Diamond | Level 26
data one;
input ID date date9. time time8.;
datetime=dhms(date,hour(time),minute(time),second(time));
format datetime datetime20.2;
cards;
1001 01NOV2020 10:01:01
;
data two;
input id seconds price;
cards;
1001	0.000	125.22
1001	0.500	122.23
1001	1.000	114.26
1001	1.500	130.60
;
data three;
	merge one two;
	by id;
	datetime_new=datetime+seconds;
	format datetime_new datetime20.3;
run;
--
Paige Miller
Andalusia
Obsidian | Level 7

@PaigeMiller 

This is how 1 looks like:

Andalusia_1-1606293451874.png

This is how 2 looks like:

Andalusia_2-1606293478279.png

This is how 3 looks like: 
Andalusia_0-1606293406227.png

@PaigeMiller You sure its doing what it should  be doing?

PaigeMiller
Diamond | Level 26

@Andalusia wrote:

@PaigeMiller 

This is how 1 looks like:

Andalusia_1-1606293451874.png

This is how 2 looks like:

Andalusia_2-1606293478279.png

This is how 3 looks like: 
Andalusia_0-1606293406227.png

@PaigeMiller You sure its doing what it should  be doing?


It must be your data source is different than mine. And possibly you have character values while my data is numeric. The program works properly with my typed in data. If you provide the data by following these instructions, then we can be sure we're both using the same data and then we can also be sure we are both getting the same answer.

--
Paige Miller
Andalusia
Obsidian | Level 7
@Well I literally just copied paste your code and ran it. I did not use it on ''my data'. Could that be it?
PaigeMiller
Diamond | Level 26

I can literally copy and paste my code, and it runs as expected. I do not get missings for data set two as you did.


Show me the LOG from what you did, paste the log as text into the window that appears when you click on the </> icon.

--
Paige Miller
Andalusia
Obsidian | Level 7

@PaigeMiller I think it has to do with your data two:

I literaly did this:

Andalusia_0-1606305801729.png

and received:

Andalusia_1-1606305824246.png

Not sure what's going on because our input is literally the same.....

PaigeMiller
Diamond | Level 26

I see the problem. Enterprise Guide runs the code differently than base SAS. In Enterprise Guide, run this:

 

data two;
infile cards expandtabs;
input id seconds price;
cards;
1001	0.000	125.22
1001	0.500	122.23
1001	1.000	114.26
1001	1.500	130.60
;
--
Paige Miller
Andalusia
Obsidian | Level 7
That looks better, Imma gonna run it on my real data sets now and get back to you in a few mins.
Andalusia
Obsidian | Level 7

@PaigeMiller So I did this:

data one;
SET pki.set1;
input ID Date date9. Time time8.;
datetime=dhms(date,hour(Time),minute(Time),second(Time));
format datetime datetime20.2;
run;
data three;
	merge one pki.set2;
	by ID;
	datetime_new=datetime+seconds;
	format datetime_new datetime20.3;
run;

And I received this:

ERROR: No DATALINES or INFILE statement.
ERROR: BY variable ID is not on input data set WORK.ONE.


I have no idea what those errors mean. Please also note that I did not use your part of two because I already have that dataset(pki.set2). By the way, those 2 datasets contain more columns than I originally showed.

PaigeMiller
Diamond | Level 26

In your data step for DATA ONE, you would not use an INPUT statement becuase the SET statement brings in the data.

 

May I make a request? From now on, please show us the entire LOG for the steps in question with nothing chopped out or deleted or edited (not the code and separately portions of the LOG).

--
Paige Miller
Andalusia
Obsidian | Level 7

@PaigeMiller Oke I'm still a rookie and new into this SAS world so I will try to keep up with your request.

 

So If I understand well I should run my code like this. Is that correct?

data one;
SET pki.set1;
datetime=dhms(date,hour(Time),minute(Time),second(Time));
format datetime datetime20.2;
run;
data three;
	merge one pki.set2;
	by ID;
	datetime_new=datetime+seconds;
	format datetime_new datetime20.3;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 18 replies
  • 1845 views
  • 0 likes
  • 5 in conversation