Posted By

webonomic on 03/31/08


Tagged

data merge macro


Versions (?)

Reusable Data Merge Macro


 / Published in: SAS
 

URL: http://jaredprins.squarespace.com/blog/2008/3/31/reusable-data-merge-macro.html

  1. /*Have 2 datasets*/
  2. data table1;
  3. INPUT id sex $ age;
  4. DATALINES;
  5. 1 F 35
  6. 2 M 50
  7. 3 F 45
  8. 3 M 24
  9. 3 F 52
  10. 1 M 44
  11. 2 F 34
  12. 1 M 40
  13. 3 F 47
  14. 2 M 35
  15. ;
  16. run;
  17.  
  18. data table2;
  19. INPUT id name $;
  20. DATALINES;
  21. 1 A
  22. 2 B
  23. 3 C
  24. ;
  25. run;
  26.  
  27. /*create Macro*/
  28. %MACRO domerge(ds1=, ds2=, resultdataset=);
  29.  
  30. proc sort data=&ds1;
  31. by id;
  32. run;
  33.  
  34. proc sort data=&ds2;
  35. by id;
  36. run;
  37.  
  38. data &resultdataset;
  39. merge &ds1 &ds2;
  40. by id;
  41. run;
  42.  
  43. %MEND domerge;
  44.  
  45. /*Use the macro*/
  46. %let t1 = table1;
  47. %let t2 = table2;
  48.  
  49. %MACRO domerge(ds1=, ds2=, resultdataset=);
  50.  
  51. proc sort data=&ds1;
  52. by id;
  53. run;
  54.  
  55. proc sort data=&ds2;
  56. by id;
  57. run;
  58.  
  59. data &resultdataset;
  60. merge &ds1 &ds2;
  61. by id;
  62. run;
  63.  
  64. %MEND domerge;

Report this snippet  

You need to login to post a comment.