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;

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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