Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting data in SAS by categorical column

I have a categorical column in my dataset which has around 20 or more categories. Now I want to create seperate dataset for all these categories.

Example: if the category is A, B, C, …. then I need table A containing A data, table B containing B data etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A classic use of the SAS Hash Object

data have;
input category $ value;
datalines;
A 1 
A 2 
A 3 
B 4 
B 5 
B 6 
C 7 
C 8 
C 9 
;

data want;
   if _N_ = 1 then do;
      dcl hash h(dataset : 'have(obs = 0)', multidata : 'Y');
      h.definekey(all : 'Y');
      h.definedone();
   end;

   do until (last.category);
      set have;
      by category;
      h.add();
   end;

   h.output(dataset : catx('_', 'table', category));
   h.clear();
run;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading