Statalist


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

AW: st: AW: Merging database


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: AW: Merging database
Date   Wed, 29 Apr 2009 21:55:55 +0200

<> 

So what is the bottom line on this -foreach in- issue? -foreach- passes on
the space-delimited elements of whatever it finds between "in" and "{", as
they are, w/o expansion of any kind. If inside the loop you get lucky and
issue a command that does the unabbreviation for you, kudos to you. But
issuing a command like -replace- that requires a single variable name, not a
-varlist-, let`s it all crash...


*************
	clear 
	set obs 10
	
	gen x1=9
	gen x2=8
	
	gen y1=9 
	gen y2=0
	
	local i 0
	foreach var in x*{
		loc ++i
		di in red "Times thru the loop: " `i'
		d `var'
// does not work as it requires a single variable	
// replace `var'=8
	}
	
	local i 0
	foreach var in x* y*{
		loc ++i
		di in red "Times thru the loop: " `i'
		d `var'
// does not work as it requires a single variable	
// replace `var'=8
	}
	
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Mittwoch, 29. April 2009 20:35
An: [email protected]
Betreff: RE: st: AW: Merging database

Correct about my typo. The example is also irrelevant, because of the
main issue, now clarified in my previous post. 

Nick 
[email protected] 

Eric A. Booth

You need to make your "V*" lowercase "v*" in your example because v1,  
v2, v3 are lowercase (in original do-file, my V's were uppercase)

  clear

  set obs 1
*obs was 0, now 1

  gen v1 = 1

  gen v2 = 2

  gen v3 = 3

foreach x in v* {
  d `x'
}


On Apr 29, 2009, at 1:20 PM, Nick Cox wrote:

> Well, it does not work for me. (I do know what a wildcard like V*  
> means
> in Stata. The point is that you need -of var- to make explicit that  
> you
> have one.)
>
> . clear
>
> . set obs 1
> obs was 0, now 1
>
> . gen v1 = 1
>
> . gen v2 = 2
>
> . gen v3 = 3
>
> . foreach v in V* {
>  2. d `v'
>  3. }
> variable V* not found
> r(111);
>
> Nick
> [email protected]
>
> Eric A. Booth
>
> On Apr 29, 2009, at 12:03 PM, Nick Cox wrote:
>
>> I have various comments on this code.
>>
>> 1. -foreach x in V*- won't work. Eric is probably thinking of
>> -foreach x of var V*- but in this case -foreach v in V1 V2 V3- takes
>> no more thought.
>>
>
> -foreach x in V*- works for fine for me.  It loops over all the
> variables V1-V3.
>
>
> *
> *   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/



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