BookmarkSubscribeRSS Feed
Kormla
Calcite | Level 5

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.

4 REPLIES 4
AncaTilea
Pyrite | Level 9

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?

Kormla
Calcite | Level 5

Hi, i only to select the first two deptcodes for each deptcode

AncaTilea
Pyrite | Level 9

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;

epr
Calcite | Level 5 epr
Calcite | Level 5

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1342 views
  • 0 likes
  • 3 in conversation