DATA Step, Macro, Functions and more

I want to create the separate datasets for each location, when number of locations are not specified

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

I want to create the separate datasets for each location, when number of locations are not specified

 

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;

 

 


Accepted Solutions
Solution
‎09-10-2017 06:11 AM
PROC Star
Posts: 768

Re: I want to create the separate datasets for each location, when number of locations are not speci

[ Edited ]
Posted in reply to Kusuma001

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


All Replies
Super User
Posts: 19,878

Re: I want to create the separate datasets for each location, when number of locations are not speci

Posted in reply to Kusuma001

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/

Solution
‎09-10-2017 06:11 AM
PROC Star
Posts: 768

Re: I want to create the separate datasets for each location, when number of locations are not speci

[ Edited ]
Posted in reply to Kusuma001

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/ 

Occasional Contributor
Posts: 5

Re: I want to create the separate datasets for each location, when number of locations are not speci

It works!! Thank you so much Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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