Statalist


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

RE: st: RE: How to get a macro with content `something'


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: How to get a macro with content `something'
Date   Wed, 27 Jan 2010 00:27:54 +0100

<>

Here is your solution. Your code raises a whole lot of questions on
different levels... 

Still, the bottom line, as always with -local-s, is that the equal sign
evaluates while a lack of it just copies strings, with several
implications...

*******
clear*
set obs 30

gen byte a=irecode(runiform(),.2, .3, .5 1)
gen byte b=irecode(runiform(),.2, .3, .5 1)
gen label ="This is the title " + string(_n)

forvalues i = 1/`c(N)'{
	local g  `g' (function y = exp(a[`i']*(x-b[`i']))/(1+
exp(a[`i']*(x-b[`i']))), /* 
 */  range(-6 6) tit(`=label[`i']'))
}

tw `g'
*******


HTH
Martin

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Alex Gamma
Sent: Mittwoch, 27. Januar 2010 00:09
To: statalist
Subject: Re: st: RE: How to get a macro with content `something'

Nick,

thanks for this clear explanation. To answer your question at the end  
(why do I think I need this?):

The problem arose from my wish to put together a graph command that  
incorporates a single graph for every observation
in a dataset and then overlays them (see code below).

So I'm looping over all observations to

1) construct the bits of the graph command for each observation  
(here: (function y = exp.......) stored in the local macros g1 g2...g 
[_N]), and

2) to cumulate these bits into the final graph command (here: the  
local macro g) that overlays all the single graphs:
- twoway (function y=exp.....)(function y=exp.....)(function  
y=exp.....)(function y=exp.....) ... -  with a total of _N  
(function...) blocks


Here's the code:
(a, b, label are variables in the dataset)

-----------------------------------
local N=_N
forvalues i = 1(1)`N' {
	local t`i' = label[`i']
	local g`i' = "(function y = exp(a[`i']*(x-b[`i']))/(1+
exp(a[`i']*(x- 
b[`i']))), range(-6 6) title(`t`i''))"
	local g = "`g'" + "`g`i''"
}

twoway "`g'"

-----------------------------------

The problem with this solution is that the graph command stored in  
macro g soon breaks the length limits for string variables.
That's why I thought it would be nice to get macro g to store not the  
actual graph commands but just macro references to them:
Macro g would then contain "`g1' `g2' `g3'... `g[_N]'" and - twoway  
"`g'" - would appropriatley expand to - twoway `g1' `g2' `g3'... `g 
[_N]' - which in turn would expand to - twoway (function[1])(function 
[2])...(function[_N]) -

Alex



Am 26.01.2010 um 18:22 Uhr schrieb Nick Cox:

> Dereferencing occurs at all levels as soon as you write down one or  
> more
> pairs of ` ' to indicate a local macro. It's not a question of peeling
> one layer off at a time. If that were so, working with nested macros
> would be more tedious.
>
> Thus although you inserted the text "`something'" inside local  
> macro A,
> as soon as you write
>
> `A'
>
> Stata looks inside, sees `something' and dereferences that in turn, so
> what you get is no surprise.
>
> If you don't want dereferencing, don't refer to a local macro  
> (until you
> need its contents, that is).
>
> You can delay dereferencing just once by using a backslash, but that
> doesn't wire in lifelong protection. It works just once, as said.
>
> As I don't recognise your need as one I've felt in 15 or so years of
> Stata programming, I am tempted to say you are not missing  
> anything. But
> let me ask directly:
>
> Why do you think you need this?
>
> Nick
> [email protected]
>
> Alex Gamma
>
> although I feel I must be overlooking something big,
> despite checking [P] and [U] and the Statalist archives,
> I wasn't able to figure out how I can simply get a local
> macro A with content `something', but without expanding
> `something' (something is a macro, too, that contains,
> let's say, foo1 [. loc something foo1]).
>
> I couldn't get it to work using "\".
> If I code
> . loc A \`something'
>
> I will get, as desired,
> . mac list _A
> _A:             `something'
>
>
> But if I use A in code, e.g.
> . loc s "`A'" + "foo2"
>
> I get
> .mac list _s
> _s:              "foo1" + "foo2"
> instead of
> _s:              `something' + "foo2"
>
> Likewise
> . di "`A'"
> returns
> foo1
> instead of
> `something'
>
> Is what I want impossible or even unnecessary?
>
> *
> *   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