BookmarkSubscribeRSS Feed
WilliamB
Obsidian | Level 7

Hello, I would like to count the number of ports of each individual in Table B to add the information in Table A.
Thanks for your help

 

 

Table A  Table B 
     
NameLoC NamePort
BARUZ BARUAS
RTYa BARUAG
DGTv DGTDS
ETOMq BARUQS
   RTYAQ
   RTYQR
   ETOMAR
   ETOMAV
     
     
Want    
     
Name LoCNB_Port  
BARUz3  
RTYa1  
DGTv

2

  
ETOMq2  
6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

How come NB_Port=1 for Name=RTY?

gamotte
Rhodochrosite | Level 12

Hello,

 

Not tested :

 

proc sql noprint;
    CREATE TABLE want AS
    SELECT a.*, count(b.PORT)
    FROM a
    LEFT JOIN b
    ON strip(upcase(b.Name))=strip(upcase(a.NAME));
quit;

According to the desired output, you may want to use count(DISTINCT PORT).

 

Edit : Forgot to group by Name. See @SwissC's answer instead.

PeterClemmensen
Tourmaline | Level 20
data TableA;   
input Name $ LoC $;
datalines; 
BARU Z
RTY  a
DGT  v
ETOM q
;

data TableB;
input Name $ Port $;
datalines;
BARU AS
BARU AG
DGT  DS
BARU QS
RTY  AQ
RTY  QR
ETOM AR
ETOM AV
;

data want(drop=rc);
    if _N_=1 then do;
        dcl hash h();
        h.definekey('Name');
        h.definedata('NB_Port');
        h.definedone();

        do until (lr);
            set TableB(keep=Name) end=lr;
            if h.find()=0 then NB_Port+1;
            else               NB_Port=1;
            h.replace();
        end;
    end;
    
    set TableA;
    NB_Port=.;

    rc=h.find();
run;
SwissC
Obsidian | Level 7
proc sql noprint;
    CREATE TABLE want AS
    SELECT a.*, nb_port
    FROM have1 a
    LEFT JOIN (select name, count(distinct(port)) as nb_port from have2 group by name) b
    ON strip(upcase(b.Name))=strip(upcase(a.NAME));
quit;
Ksharp
Super User
data TableA;   
input Name $ LoC $;
datalines; 
BARU Z
RTY  a
DGT  v
ETOM q
;

data TableB;
input Name $ Port $;
datalines;
BARU AS
BARU AG
DGT  DS
BARU QS
RTY  AQ
RTY  QR
ETOM AR
ETOM AV
;
proc sql;
create table want as
select a.name,a.loc,count(*) as n
 from tablea as a left join tableb as b
  on a.name=b.name
   group by a.name,a.loc ;
quit;
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
  • 6 replies
  • 1736 views
  • 0 likes
  • 6 in conversation