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;
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;
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/
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;
It works!! Thank you so much 🙂
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.