Solved
Contributor
Posts: 28

# sas code for matching sample

I have two data sets- test firms and control firms. I need to match these two data sets based on year and the minimum difference in their size.

Test Firms

firm year size

A    2000  10

B    2005   25

Control Firms

firm year size

S   2000  5

T    2000  7

U   2000  12

V   2000  9

M   2005  20

N    2005  22

O   2005  27

The expected output

firm year size Control_firm

A    2000  10    v

B    2005   25   o

I would appreciate if someone provides me the code for this.

Accepted Solutions
Solution
‎09-12-2012 01:32 PM
Super Contributor
Posts: 1,636

## Re: sas code for matching sample

data Test;

input firm \$ year size;

cards;

A    2000  10

B    2005   25

;

data Control;

input firm \$ year size;

cards;

S   2000  5

T    2000  7

U   2000  12

V   2000  9

M   2005  20

N    2005  22

O   2005  27

;

proc sql;

create table want(drop=diff) as

select a.*,b.firm as c_firm,abs(a.size-b.size) as diff

from test a,control b

where a.year=b.year

group by a.firm

having diff=min(diff);

proc print data=want;run;

Message was edited by:Linlin

All Replies
Solution
‎09-12-2012 01:32 PM
Super Contributor
Posts: 1,636

## Re: sas code for matching sample

data Test;

input firm \$ year size;

cards;

A    2000  10

B    2005   25

;

data Control;

input firm \$ year size;

cards;

S   2000  5

T    2000  7

U   2000  12

V   2000  9

M   2005  20

N    2005  22

O   2005  27

;

proc sql;

create table want(drop=diff) as

select a.*,b.firm as c_firm,abs(a.size-b.size) as diff

from test a,control b

where a.year=b.year

group by a.firm

having diff=min(diff);

proc print data=want;run;

Message was edited by:Linlin

Contributor
Posts: 28

## Re: sas code for matching sample

Thanks a lot Linlin.

Super Contributor
Posts: 339

## Re: sas code for matching sample

Hi,

data test_firms;

input firm \$ year size;

datalines;

A    2000  10

B    2005   25

;

data control_firms;

input control_firm \$ year cf_size;

datalines;

S   2000  5

T    2000  7

U   2000  12

V   2000  9

M   2005  20

N    2005  22

O   2005  27

;

data want(drop=cf_size diff);

merge test_firms   (in=in_tf)

control_firms(in=in_cf);

by year;

retain diff .;

if in_tf and in_cf then

do;

diff=min(diff,size-cf_size);

if last.year then

output;

end;

run;

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

Posts: 3,167

## Re: sas code for matching sample

Another SQL approach:

data h;

input firm\$ year size;

cards;

A    2000  10

B    2005   25

;

data hc;

input firm\$ year size;

cards;

S   2000  5

T    2000  7

U   2000  12

V   2000  9

M   2005  20

N    2005  22

O   2005  27

;

proc sql;

select h.*,hc.firm as firm_control, abs(h.size-hc.size) as dif from h, hc where h.year=hc.year

group by h.firm, h.year having dif=min(dif);

quit;

Haikuo

🔒 This topic is solved and locked.