Here's a very similar question
My question is a bit different from the one in the above link.
Background
I have a data set contains hourly data. So each object has 24 records per day. Now I want to create K
new columns represents next 1,2,...K
hourly records for each object. If not exist, replace them with missing values.
K
is dynamic and is defined by users.
The original order must be preserved. No matter it's guaranteed in the data steps or by using sorting in the end.
I'm looking for an efficient way to achieve this.
Example
Original data:
Object Hour Value
A 1 2.3
A 2 2.3
A 3 4.0
A 4 1.3
Given K = 2
, desired output is
Object Hour Value Value1 Value2
A 1 2.3 2.3 4.0
A 2 2.3 4.0 1.3
A 3 4.0 1.3 .
A 4 1.3 . .
Possible solutions
- sort in reverse order -> obtain previous k records -> sort them back.
When the no. of observation is large, this shouldn't be an ideal way.
proc expand
. I don't familiar with it cause it's never licensed on my pc.Using
point
in data step.retain
statement inside data step. I'm not sure how this works.