data trials1;
input id deptcode salary;
datalines;
1001 FRM 23000
1002 GMM 45090
1003 DFG 23480
1004 TTM 34200
1005 DEE 34000
1006 FRM 45000
1007 GMM 34700
1008 DFG 43000
1009 DFG 23000
1010 TTM 34700
1011 DEE 45022
1012 FRM 90111
1013 GMM 76000
1014 DFG 47800
1015 TTM 45600
1016 DEE 34890
1017 FRM 34500
1018 GMM 35700
1019 DFG 48000
1020 TTM 36700
;
i have a sample data, i am trying to write a code that selects two deptcodes each from the data, any help please, thanks.
First, you need a dollar sign to tell SAS that department is character;
data trials1;
input id deptcode $ salary;
datalines;
1001 FRM 23000
1002 GMM 45090
...
;
OK, so, what do you need? what two departments would you like to select?
Your question is very vague. Do you need the two departments with the top salary?
or?
Hi, i only to select the first two deptcodes for each deptcode
You can try this:
proc sort data = trials1;by deptcode;run;
data want;
set trials1;
by deptcode;
retain cnt;
if first.deptcode then cnt = 1;
else cnt ++ 1;
if cnt < 3;
run;
Try some thing like this
data trials1;
input id deptcode $ salary;
datalines;
1001 FRM 23000
1002 GMM 45090
1003 DFG 23480
1004 TTM 34200
1005 DEE 34000
1006 FRM 45000
1007 GMM 34700
1008 DFG 43000
1009 DFG 23000
run;
proc sql;
create table want as select * from trials1 group by deptcode having count(deptcode)=2 order by id;
quit;
run;
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.