BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All

My data is as follows

 

IDA       IDB     Cat     VarA

A            1         1        7

A             1         1       9

B           1           1       7

B           1           1      10

B           2           1      11

A           1           2      12

A            1          2       14

A            2          2       16

C          1            3       12

C1        1           3        14

 

I want the following table as output removing the duplicates and just keeping the first observation for IDA IDB for each CAT

 

IDA       IDB     Cat     VarA

A            1         1        7

B           1           1       7

B           2           1      11

A           1           2      12

A            2          2       16

C          1            3       12

 

The code I wrote was

proc sort data = have; by IDA IDB Cat ; run;

 

data want ; set have

by IDA IDB Cat ;

if first.Cat then output ;

run;

 

Am I making a mistake somewhere?

run;

    

5 REPLIES 5
tarheel13
Rhodochrosite | Level 12

next time please post the data as datalines.

RandyStan
Fluorite | Level 6

 

Sincere apologies

 

  Row         IDA       IDB     Cat     VarA

      1              A            1         1        7

      2              A             1         1       9

      3              B           1           1       7

      4             B           1           1      10

     5               B           2           1      11

      6              A           1           2      12

      7              A            1          2       14

       8              A            2          2       16

       9               C          1            3       12

      10                 C       1           3        14

 

Row         IDA       IDB     Cat     VarA

      1              A            1         1        7

       3              B           1           1       7

      5               B           2           1      11

      6              A           1           2      12

       8              A            2          2       16

       9               C          1            3       12

     

 

 

tarheel13
Rhodochrosite | Level 12

Hey I meant to post it as a data step. You can see an example in my other reply to you. Also, you can post code by clicking the running man icon. 

tarheel13
Rhodochrosite | Level 12

I don't see C1 in your desired output. Is there a reason that was excluded? 

data have;
   input IDA $ IDB $ Cat $ VarA;
   datalines;
A 1 1 7
A 1 1 9
B 1 1 7
B 1 1 10
B 2 1 11
A 1 2 12
A 1 2 14
A 2 2 16
C 1 3 12
C1 1 3 14
;

proc print;
run;

proc sort data=have out=want nodupkey;
   by IDA IDB Cat;
run;

You can try the nodupkey option in proc sort.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 630 views
  • 0 likes
  • 3 in conversation