Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SAS-Update history records type 2

Posted on 2012-03-14
5
Medium Priority
?
326 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
ID: 37723494
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
ID: 37723800
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
ID: 37723814
oh yes, theartfuldazzler thanks for pointing that out :)
0
 

Author Comment

by:sam2929
ID: 37726146
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 2000 total points
ID: 37727494
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

580 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