BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kusuma001
Calcite | Level 5

 

Question:

 

if user dont know how many locations are there in input, then how can we write a code in SAS

 

Task :

 

data one;

input emp_id  name$ loc$;

datalines;

1 srinivas bang

2 sudhakar bang

3 qqqq hyd

4 hhhh hyd

5 bhanu mumbai

6 jjjjj mumbai

;

run;

 

Solution:;

 

DATA Bang;

 set one(where=(loc='bang'));

run;

DATA Hyd;

 set one(where=(loc='hyd'));

run;

DATA Mumbai;

 set one(where=(loc='mumbai'));

run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If you dont know how any locations are in your data step, this HASH object solution works well.

 

data one;
input emp_id  name$ loc$;
datalines;
1 srinivas bang
2 sudhakar bang
3 qqqq hyd
4 hhhh hyd
5 bhanu mumbai
6 jjjjj mumbai
;

proc sort data=one;
   by loc;
run;

data _null_;
   if _n_=1 then do;
   if 0 then set one;
   declare hash h(dataset:"one(obs=0)", multidata:'y');
      h.definekey(all:'y');
      h.definedata(all:'y');
      h.definedone();
   end;
   do until(last.loc);
      set one;
      by loc;
      h.add();
   end;
   h.output(dataset:loc);
   h.clear();
run;

 

http://sasnrd.com/split-data-by-group/ 

View solution in original post

3 REPLIES 3
Reeza
Super User

If you search 'split data into subsets' you'll find many solutions. Note that this is rarely recommended in SAS

 

http://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/

PeterClemmensen
Tourmaline | Level 20

If you dont know how any locations are in your data step, this HASH object solution works well.

 

data one;
input emp_id  name$ loc$;
datalines;
1 srinivas bang
2 sudhakar bang
3 qqqq hyd
4 hhhh hyd
5 bhanu mumbai
6 jjjjj mumbai
;

proc sort data=one;
   by loc;
run;

data _null_;
   if _n_=1 then do;
   if 0 then set one;
   declare hash h(dataset:"one(obs=0)", multidata:'y');
      h.definekey(all:'y');
      h.definedata(all:'y');
      h.definedone();
   end;
   do until(last.loc);
      set one;
      by loc;
      h.add();
   end;
   h.output(dataset:loc);
   h.clear();
run;

 

http://sasnrd.com/split-data-by-group/ 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1692 views
  • 0 likes
  • 3 in conversation