Solved

SAS-Update history records type 2

Posted on 2012-03-14
5
300 Views
Last Modified: 2012-03-30
Hi,
We need to fix some history data right

This is type 2 dimension we have issue with first 2 rows


VALID_FROM_DTTM          VALID_TO_DTTM        emp_id  emp_key
26AUG2011:23:59:59      12OCT2011:16:26:56     101     1
10OCT2011:23:59:59      07NOV2011:23:59:58     101     201
07NOV2011:23:59:59      12DEC2011:23:59:58    101      302
12DEC2011:23:59:59      13DEC2011:23:59:58    101      801
13DEC2011:23:59:59      22DEC2011:23:59:58    101      10001
22DEC2011:23:59:59      23DEC2011:23:59:58    101      1000005
23DEC2011:23:59:59      25DEC2011:23:59:58    101     1000008
25DEC2011:23:59:59      27DEC2011:23:59:58    101     10000011
27DEC2011:23:59:59      01JAN2012:23:59:58   101     10000013
01JAN2012:23:59:59      02JAN2012:23:59:58   101     10000022
02JAN2012:23:59:59      20JAN2012:23:59:58   101     10000045
20JAN2012:23:59:59      22JAN2012:23:59:58   101     10000067
22JAN2012:23:59:59      31DEC9999:00:00:00   101     100000987


Isuue is Second row VALID_FROM_DTTM should be 12OCT2011:16:26:57
so it should looks like

26AUG2011:23:59:59      12OCT2011:16:26:56     101     1
12OCT2011:16:26:57      07NOV2011:23:59:58     101     201

We have issue with some other emp_id too so we want to update
VALID_FROM_DTTM where VALID_TO_DTTM  <> to VALID_TO_DTTM -1 sec
based upon emp_id
0
Comment
Question by:sam2929
  • 3
5 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
Comment Utility
you appear to know what your problem is already, and your solution, so you are looking for the code to implement your solution?

anyway have you tried loading duplicate records at initial load for example? I remembered encountering this problem when I did this some time back, the SCD Type2 Generator has a code which will adjust the date/datetime if it detects duplicate records being loaded...

i would do something like:
proc sort data = [input]; by EMP_ID VALID_FROM_DTTM; run;

data [output];
  set [input];
  by EMP_ID;
  retain PREV_TO_VAL;
  if first.EMP_ID then do;
    PREV_TO_VAL = VALID_TO_DTTM;
  end;
  else do;
    VALID_FROM_DTTM = PREV_TO_VAL + 1;
  end;
run;

as usual, please do back up your original data so that it can be restored if need be. also, please do test the code and check the output to ensure that this is what you really wanted
0
 
LVL 11

Expert Comment

by:theartfuldazzler
Comment Utility
Hi

Iowa's code will work with a slight change - one needs to reset the value of PREV_TO_VAL:

proc sort data = [input]; by EMP_ID VALID_FROM_DTTM; run;

data [output];
  set [input];
  by EMP_ID;
  retain PREV_TO_VAL;
  if first.EMP_ID then do;
    PREV_TO_VAL = VALID_TO_DTTM;
  end;
  else do;
    VALID_FROM_DTTM = PREV_TO_VAL + 1;
     PREV_TO_VAL = VALID_TO_DTTM;
  end;
DROP PREV_TO_VAL;
run;

Open in new window


I actually prefer using the LAG function for these types of code:

proc sort data = [input]; by EMP_ID VALID_FROM_DTTM; run;

DATA [output];
   set [input];
  by EMP_ID;
   IF NOT First.EMP_ID THEN
      VALID_FROM_DTTM = LAG(VALID_TO_DTTM) + 1;
RUN;
      

Open in new window

0
 
LVL 14

Expert Comment

by:Aloysius Low
Comment Utility
oh yes, theartfuldazzler thanks for pointing that out :)
0
 

Author Comment

by:sam2929
Comment Utility
can't we modify this code to do changes just where
where VALID_TO_DTTM  <> to VALID_TO_DTTM -1 sec
0
 
LVL 14

Accepted Solution

by:
Aloysius Low earned 500 total points
Comment Utility
yes, you can, but it will depend on the underlying value... if there's decimals, although not displayed, the value may not match and hence every value will be updated...

for further understanding, SAS stores date and datetime values as numbers. however, these are not entire whole numbers, but decimals are involved as well. hence, even if we were to put a check for VALID_FROM_DTTM <> to VALID_TO_DTTM -1, it might end up all values being updated, although you might not see it.

to add in this condition, do it within the else do statements:
else do;
    if VALID_FROM_DTTM + 1 ne PREV_TO_VAL then
        VALID_FROM_DTTM = PREV_TO_VAL + 1;
    PREV_TO_VAL = VALID_TO_DTTM;
end;
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now