Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. /*-----------------------------------------------------------------------------|
  2. | Description : Macro that compares two tables, record by record, based on a |
  3. | list of ID variables. |
  4. | Assumptions : Each table has at least one ID variable, and ID variables must |
  5. | have the same name between tables. |
  6. | N.B.: This macro is based on the suggestion made in the |
  7. | excellent book "Carpenter's Guide to Innovative SAS |
  8. | Techniques" by Art Carpenter, pages 198 through 200. |
  9. | Parameters : BaseTable - Is the reference table. |
  10. | ReferenceTable - Corresponds to the table that is expected |
  11. | to be the same as the reference table. |
  12. | DifferencesTable - The name of the output table that will |
  13. | contain the differences found between both |
  14. | tables. |
  15. | IDVariables - A list of variables that allows to map each |
  16. | record between tables. |
  17. | Output : A table with the differences found between records and based |
  18. | on the ID variables. |
  19. |-----------------------------------------------------------------------------*/
  20. /* Examples:
  21. * We create the sample datasets. ;
  22. DATA BASE_TABLE;
  23. INFILE DATALINES
  24. DSD DLM = ','
  25. MISSOVER;
  26. LENGTH ID
  27. X $2;
  28. INPUT ID $
  29. X $
  30. Y;
  31. DATALINES;
  32. 01,AA,100
  33. 02,BB,200
  34. 03,CC,20.5
  35. 04,DD,800
  36. ;
  37. RUN;
  38. DATA REFERENCE_TABLE;
  39. INFILE DATALINES
  40. DSD DLM = ','
  41. MISSOVER;
  42. LENGTH ID
  43. X $2;
  44. INPUT ID $
  45. X $
  46. Y;
  47. DATALINES;
  48. 01,AA,100
  49. 02,B,200
  50. 03,CC,8.5
  51. 04,DD,800
  52. ;
  53. RUN;
  54. * Ex_01;
  55. %MCompareDataSets(BaseTable = BASE_TABLE,
  56. ReferenceTable = REFERENCE_TABLE,
  57. DifferencesTable = DIFFERENCES_TABLE,
  58. IDVariables = ID)
  59. * Output: Creates a table 'DIFFERENCES_TABLE' with the records and variables
  60. that were found to be different (i.e., records 02 and 03 and variables
  61. X and Y, respectively). ;
  62. */
  63. /*-----------------------------------------------------------------------------|
  64. | Date Author Description |
  65. |------------------------------------------------------------------------------|
  66. | June 22, 2017 Cesar R. Urteaga-Reyesvera Creation. |
  67. |-----------------------------------------------------------------------------*/
  68. %MACRO MCompareDataSets(BaseTable = /* Reference table. */,
  69. ReferenceTable = /* New table which is expected to be
  70. the same as the base table. */,
  71. DifferencesTable = /* Table with the differences
  72. between the above tables. */,
  73. IDVariables = /* A list of variables to identify
  74. de records that must be compared.
  75. */
  76. );
  77. * Since we use the ID variables, we need to sort them in order to compare
  78. them. ;
  79. PROC SORT DATA = &BaseTable.; BY &IDVariables.; RUN;
  80. PROC SORT DATA = &ReferenceTable.; BY &IDVariables.; RUN;
  81. PROC COMPARE BASE = &BaseTable.
  82. COMPARE = &ReferenceTable.
  83. OUT = TEMPORAL_01
  84. OUTBASE OUTCOMP
  85. NOVALUES LISTVAR
  86. OUTNOEQUAL; /* We only care about the values which are different
  87. between both tables. */
  88. ID &IDVariables;
  89. RUN;
  90. * We change the table from wide format to long format. ;
  91. PROC SORT DATA = TEMPORAL_01;
  92. BY &IDVariables.
  93. _OBS_;
  94. RUN;
  95. PROC TRANSPOSE DATA = TEMPORAL_01
  96. OUT = TEMPORAL_02(DROP = _LABEL_
  97. RENAME = (_NAME_ = VARIABLE));
  98. BY &IDVariables.
  99. _OBS_;
  100. * Allows to list all variables so as to compare them. ;
  101. VAR _NUMERIC_
  102. _CHARACTER_;
  103. * This statement is used to name the two new columns. ;
  104. ID _TYPE_;
  105. RUN;
  106. * Finally, we get the output table with the differences and delete the
  107. temporal tables. ;
  108. DATA &DifferencesTable.;
  109. SET TEMPORAL_02(WHERE = (VARIABLE ~= "_TYPE_" &
  110. BASE ~= COMPARE)); /* We only want the
  111. variables that are
  112. different. */
  113. RUN;
  114. PROC SQL; DROP TABLE TEMPORAL_01, TEMPORAL_02; QUIT;
  115. %MEND MCompareDataSets;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement