match firms

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

match firms

I need to find a match for the test sample from the control sample. I need to have the following matching criteria:

(1) test firms and control firms should be from the same year

(2) first preference would be given to control firms whose size are lower than the test firms. If such a match is not found, then select the control firms whose size is greater than the test firms.

Test Sample

firm year size

A    2000 10

B    2005  25

CONTROL Sample

firm year size

s     2000 5

t      2000 7

u     2000 12

v      2000 15

m    2005 27

n     2005 30

Desired Output

firm year size control_firms

A    2000 10    t

B    2005  25   m

I got the following code from a generous SAS expert from this community. Can anyone please help me to modify the code?

proc sql;

   create table temp as

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

    from test a,control b

      where a.year=b.year

    order by a.firm,diff;

   quit;

  data temp1;

      set temp;

   by firm;

   if first.firm;

proc sql;

   create table want as

     select a.*,c_firm

       from test a,temp1 b

         where  a.firm=b.firm;

proc print data=want;run;


Accepted Solutions
Solution
‎09-12-2012 02:57 PM
Respected Advisor
Posts: 3,124

Re: match firms

Here you go:

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 15

m    2005 27

n     2005 30

;

proc sql;

create table want (drop=dif) as

select h.*,hc.firm as firm_control, case when h.size>hc.size

                                            then 1/(hc.size-h.size)

                                                                  else hc.size-h.size

                                                                  end as dif from h, hc where h.year=hc.year

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

quit;

Haikuo

View solution in original post


All Replies
Solution
‎09-12-2012 02:57 PM
Respected Advisor
Posts: 3,124

Re: match firms

Here you go:

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 15

m    2005 27

n     2005 30

;

proc sql;

create table want (drop=dif) as

select h.*,hc.firm as firm_control, case when h.size>hc.size

                                            then 1/(hc.size-h.size)

                                                                  else hc.size-h.size

                                                                  end as dif from h, hc where h.year=hc.year

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

quit;

Haikuo

Contributor
Posts: 28

Re: match firms

Thank you for the code.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 313 views
  • 1 like
  • 2 in conversation