Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: RE: psmatch2 with the if condition

 From Roberto Mura To statalist@hsphsun2.harvard.edu Subject Re: st: RE: psmatch2 with the if condition Date Mon, 22 Mar 2010 13:13:35 +0000

Nick,
Thank you for your kind reply. The loop is not the main issue. Even if I do not specify any loop but simply code:
use "C:\AAA.dta", clear
catenate condition1 =  country sic2
gen quotedcondition1=1 if quotedcompany==1
replace quotedcondition1=0 if quotedcompany==0 & diversification_orig==1
gen quotednumerator=.
gen quoteddenomenator=.
gen privatenumerator=.
gen privatedenominator=.
egen g=group(condition1)
bysort condition1: gen Ncondition1=_N

psmatch2 quotedcondition1 if g==1, pscore(size) noreplacement

I still find that psmatch2 uses companies which do not belong to group 1 to look for matches. As a matter of fact, it is assigning _id values outside the range of group 1 (there are just 15 firms in it but psmatch2 reports values like 877). Also, I am not blaming psmatch2 for anything and I never said I discovered a bug. I am just trying to understand what I am doing wrong. Also, please note that my code is very similar to the one reported in the help of psmatch2 which I report below for simplicity:
Matching within strata
The following code illustrates how to match within exact cells and then calculate the average effect for the whole population.

g att = .
egen g = group(groupvars)
levels g, local(gr)
qui foreach j of local gr {
psmatch2 treatvar varlist if g==`j', out(outvar)
replace att = r(att) if  g==`j'
}
sum att
Kind regards
R

At 11:22 22/03/2010, you wrote:
You refer to various user-written programs here. Remember that you are
asked to explain where user-written programs mentioned in a post come
from.

-catenate- is from SSC. It has long since morphed into, and been
superseded by, the official -egen- function -concat()-. However, its use
here is not problematic.

More importantly, -psmatch2- is a very often used command from SSC. I
haven't used it, but my guess is very much that you are misunderstanding
something rather than that you have discovered a bug.

Note that Stata is not doing anything independently of the commands you
issue, contrary to some of your wording here.

I haven't tried to understand your detailed logic, but what seems much
more likely than a problem with -psmatch2- is that you are
misunderstanding what your -while- loops are doing.

You have two loops

local j=1
while `j' <= g {
...
local i=1
while `i' < Ncondition1 {
...
}
}

Both involve looping until a value stored in a variable is reached, -g-
in the outer loop and -Ncondition1- in the inner loop. With this kind of
programming what Stata does is look at the value of the named variable
_in the first observation_. This is documented at

FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if
qualifier
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J.
Wernow
6/00    I have an if command in my program that only seems
to evaluate the first observation, what's going on?
http://www.stata.com/support/faqs/lang/ifqualifier.html

So, it's as if you had programmed

local j=1
while `j' <= g[1] {
...
local i=1
while `i' < Ncondition1[1] {
...
}
}

It's evident that is definitely not what you want and leading to your
loop terminating before the work you want is done and that you are
unjustly putting the blame on -psmatch2-. It may be that your loop just
goes round once and does not get beyond -g[1]- which is 1.

Nick
n.j.cox@durham.ac.uk

Dr. Roberto Mura, Ph.D. [edited]

I have a cross section of companies, some listed some private.
I am trying to find a matching private (undiversified) firm for each
listed one, on the basis of company size. Also, since my dataset is
cross country, I am trying to control for country and SIC code as
well. To this end, I have created a condition, called -condition1-, to
divide the sample into a set of clusters:

catenate condition1 =  country sic2

Now, once a match is found, I need Stata to report some accounting
data for the matched private firm on the same row as the  listed firm.
I have tried to create a "self-fulfilling" nested loop so that I
don't need to specify beforehand the total number of clusters
(-condition1-) and also because the total number of firms changes with
different clusters (-Ncondition1- below).  The problem that I am facing
is that -psmatch2- does not seem to understand the -if- condition.
I have uploaded the dataset (for version 11 and for version 9 of
Stata) and the do file in my website under
www.robertomura.com/psmatch2/ if you want to take a look.
Here is what I do:

use "D:\AAA.dta", clear
catenate condition1 =  country sic2
gen quotedcondition1=1 if quotedcompany==1
replace quotedcondition1=0 if quotedcompany==0 & diversification_orig==1
gen quotednumerator=.
gen quoteddenomenator=.
gen privatenumerator=.
gen privatedenominator=.
egen g=group(condition1)
bysort condition1: gen Ncondition1=_N

local j=1
while `j' <=g {
psmatch2 quotedcondition1 if g==`j', pscore(size) noreplacement
replace quotednumerator=fnetincome if _nn==1
replace quoteddenomenator=shareholdersfundsnew if _nn==1
replace privatenumerator=fnetincome if _n1==_id
replace privatedenominator=shareholdersfundsnew if _n1==_id

local i=1
while `i' <Ncondition1 {
replace privatenumerator=fnetincome[_n+`i'] if _n1==_id[_n+`i']
replace privatedenominator=shareholdersfundsnew[_n+`i'] if
_n1==_id[_n+`i']
replace privatenumerator=fnetincome[_n-`i'] if _n1==_id[_n-`i']
replace privatedenominator=shareholdersfundsnew[_n-`i'] if
_n1==_id[_n-`i']
local i = `i' + 1
}
local j = `j' + 1
}

The problem seems to lie in

psmatch2 quotedcondition1 if g==`j', pscore(size) noreplacement

What seems to be happening is that the matching is done only for the
first 3 firms of the first cluster and then Stata is mis-assigning
the _id since the numbers do not follow the clustering but rather,
Stata is applying the -psmatch2- considering the sample as a whole. If,
for instance, I change the if condition into:

psmatch2 quotedcondition1 if g==1, pscore(size) noreplacement

then Stata is not matching inside the cluster (condition1) but again
it is treating the sample as a whole. In other words the "if"
condition does not appear to be used at all by Stata.

I have tried both version of psmatch2. The one available through SSC
(version 3.1.5 2may2009) and the more recent one available from
http://leuven.economists.nl/stata (4.0.1)
I have Stata/MP 11.0 for Windows (64-bit x86-64)  running on XP Pro
64. I also tried with the older version I had, Stata/SE 9.2 for
Windows 64-bit x86-64and things don't change.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/