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/ 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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