SAS-Update history records type 2

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
sam2929Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aloysius LowCommented:
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
theartfuldazzlerCommented:
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
Aloysius LowCommented:
oh yes, theartfuldazzler thanks for pointing that out :)
0
sam2929Author Commented:
can't we modify this code to do changes just where
where VALID_TO_DTTM  <> to VALID_TO_DTTM -1 sec
0
Aloysius LowCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.