SAS Macro to split dataset by observations


/ Published in: SAS
Save to your folder(s)



Copy this code and paste it in your HTML
  1. options nosource nomprint nomlogic nosymbolgen;
  2.  
  3. /**
  4. Usage of the Macro-
  5. %splitdsnbyobs(DatasetName, No ofobservation to split by)
  6. **/
  7.  
  8. /* creating a dataset with 100000 observations*/
  9.  
  10. %macro splitdsnbyobs(dsn,splitby);
  11.  
  12. data _null_;
  13. set &dsn nobs=num;
  14. call symput('no_obs',num);
  15. run;
  16.  
  17. %let no_obs=&no_obs; /*Get the number of observations in &dsn*/
  18.  
  19. /* keep the observations from firstobs= and obs=*/
  20. %do i=1 %to %sysfunc(ceil(&no_obs/&splitby));
  21. data &dsn.&i.;
  22. set &dsn (firstobs=%sysfunc(floor(%eval((&i.-1)*&splitby.+1))) obs=%sysfunc(ceil(%eval(&i * &splitby.))));
  23. run;
  24. %end;
  25. %mend splitdsnbyobs;
  26.  
  27. /* Eg. Create a Dsn with 100 observations */
  28. data loops;
  29. do i=1 to 100;
  30. output;
  31. end;
  32. run;
  33.  
  34. /*Now call the macro to split the observations every 20 records*/
  35. %splitdsnbyobs(loops,20);
  36.  
  37. /*
  38. Read more: http://sastechies.blogspot.com/2009/11/sas-macro-to-split-dataset-by-number-of.html
  39. */

URL: http://sastechies.blogspot.com/2009/11/sas-macro-to-split-dataset-by-number-of.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.