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.

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