Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

RE: st: Problem with temporary variable and macro suffix


From   Joe Canner <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Problem with temporary variable and macro suffix
Date   Thu, 29 Aug 2013 16:55:39 +0000

Thomas,

It also "works" in Stata 12, but it only generates variables __000002, __000003, and __000004.  The code you provided creates three macros called newlement1, newelement2, and newelement3, which have values "__000002","__000003", and "__000004", respectively.  Is that what you wanted?

Or did you want these new variables to have suffixes?  If so then you would need something like:

foreach a in element1 element2 element3 {
             tempvar new`a'
             gen `new`a''`a' = .
         }

Although I'm not sure this is what you really want either.

Regards,
Joe Canner
Johns Hopkins University School of Medicine


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Thursday, August 29, 2013 12:29 PM
To: [email protected]
Subject: Re: st: Problem with temporary variable and macro suffix

Works for me (in Stata 10.1)

Not quite documented, but temporary variables (used to) have names beginning with __ and followed by 6 characters assigned by Stata, so that you can (could) add suffixes in practice, although somewhere Stata says that you shouldn't assume that you can do that.

That aside, this is idiomatic

foreach a in element1 element2 element3 {
      tempvar this
      gen `this' = .
      local all `all' `this'
}

followed typically by loops over `all'.


Nick
[email protected]


On 29 August 2013 17:21, Thomas Lux <[email protected]> wrote:
> I have a little problem with temporary variables in my program.
>
> I want to create temporary variables with some foreach-elements as a suffix.
> I did:
>
>         foreach a in element1 element2 element3 {
>             tempvar new`a'
>             gen `new`a'' = .
>         }
>
> But this does not work.
> I there any other way to do it?
>
>
> Best,
> Thomas
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index