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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.