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 🙂
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.