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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 783 views
  • 0 likes
  • 6 in conversation