[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: AW: RE: Re: loop code problem |

Date |
Tue, 24 Mar 2009 09:09:09 +0100 |

<> The problem with your code is the last line inside the loop: You specifically instruct Stata to replace "matchernum" with "lister" and subsequently complain that it contains this very number. Why not instruct Stata to replace with "name"? Also, you do not specify an "in" qualifier for the last line, so Stata by default -replace-s the whole vector of "matchernum"s with the value 9118, which is the only one that matches in your example data. (I assume that the "matcheri" and "matcherj" are filled with ones because you copied from the full dataset of 459 observations. when I rerun your code, only observation 1 and 5 get a one.) I would advise you to look at -merge- as a much better solution. Split the dataset between the original and the to-be-matched firms and then -merge- them back. HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Li, Ihsuan Gesendet: Dienstag, 24. März 2009 00:54 An: statalist@hsphsun2.harvard.edu Betreff: st: RE: Re: loop code problem Hi, I am sorry about the variable names. Here is the actual codes and results. There are 459 observations, listed are results for eight observations. Instead of a column of the names of the matched companies under matchernum, I get one same number under it. For example, in row five, cusip #40707105, the company is Hamilton bros, its unique id is 9118, it was matched to Atna (row1). Two questions really: 1. why is it returning identical number under matchernum? 2. how can I get it return the "name" instead of the id? I hope I am making sense now. Thanks! Ihsuan ---------------------------------------- gen matcheri=. gen matcherj=. gen matchernum=. forvalues i=1(1) 459 { forvalues j=`i'(1) 459 { replace matcheri=1 in `i' if lister[`i']== id[`j'] & lister[`i']!=. & id[`j'] !=. replace matcherj=1 in `j' if lister[`i']== id[`j'] & lister[`i']!=. & id[`j'] !=. replace matchernum=lister[`i'] if lister[`i']== id[`j'] & lister[`i']!=. & id[`j'] !=. } } Result: cusip name lister id matcheri matcherj matchernum 1 Ahtna 9118 4837 1 . 9118 2 Aleut 4654 4885 1 . 9118 3 Arctic Slope 278 9133 1 . 9118 4 Bering Strait 3506 4833 1 . 9118 40707105 HAMILTON BROS . 9118 . 1 9118 81611405 SEITEL INC . 4742 . 1 9118 82524102 SHOREWOOD CORP . 7864 . 1 9118 82770101 SILVER DINER . 4235 . 1 9118 -------------------------------------- -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Martin Weiss Sent: Monday, March 23, 2009 4:24 PM To: statalist@hsphsun2.harvard.edu Subject: st: Re: loop code problem <> Where in your dataset is this variable "lister" that you condition on in the loop? And where do you actually fill the "matchedname" variable whose contents you complain about? HTH Martin _______________________ ----- Original Message ----- From: "Li, Ihsuan" <ihsuan.li@mnsu.edu> To: <statalist@hsphsun2.harvard.edu> Sent: Monday, March 23, 2009 10:00 PM Subject: st: loop code problem > Hi statalisters: > > I can't figure out the correct coding to create a new variable > "matchedname". My data was retrieved using propensity score matching > technique and it returned _id which is unique to each firm. Each firm also > has an unique cusip number. The first three firms (atna, Sealaska, and > aleut) were matched to three other firms (petrox, Oregon, and ripley). I > know that from the _n1 column, which lists the matched company's > particular id. > > What I am trying to do is create string variable "matchedname" that lists > the names of the three firms that were matched to the first three > observations. > > > Example of my data: > > Firm cusip _n1 _id matchedname > atna 100 2569 9521 > sealaska 101 4526 9823 > aleut 104 325 357 > petrox 10001 . 2569 > oregon corp 10002 . 4526 > ripley 10003 . 325 > > > I tried the following code, and it returned the same _n1 on the > matchedname column instead: > > . gen matcheri=. > (6 missing values generated) > > . gen matcherj=. > (6 missing values generated) > > . gen matchernum=. > (6 missing values generated) > > . forvalues i=1(1) 6 { > 2. forvalues j=`i'(1) 6 { > 3. replace matcheri=1 in `i' if lister[`i']== id[`j'] & lister[`i']!=. & > id[`j'] !=. > 4. replace matcherj=1 in `j' if lister[`i']== id[`j'] & lister[`i']!=. & > id[`j'] !=. > 5. replace matchernum=lister[`i'] if lister[`i']== id[`j'] & > lister[`i']!=. & id[`j'] !=. > 6. } > 7. } > > > What is wrong with this code? > > > Thanks in advance. > > Ihsuan > > > > > * > * 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/ * * 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/

**Follow-Ups**:**st: RE: AW: RE: Re: loop code problem***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**References**:**st: loop code problem***From:*"Li, Ihsuan" <ihsuan.li@mnsu.edu>

**st: Re: loop code problem***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**st: RE: Re: loop code problem***From:*"Li, Ihsuan" <ihsuan.li@mnsu.edu>

- Prev by Date:
**Re: st: [Graphics] how to set the length of an axis?** - Next by Date:
**st: Spatial lag model with time dimension in STATA** - Previous by thread:
**st: RE: Re: loop code problem** - Next by thread:
**st: RE: AW: RE: Re: loop code problem** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |