Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Macro questions.

From   [email protected]
To   [email protected]
Subject   Re: st: RE: Macro questions.
Date   Tue, 10 Aug 2004 18:59:35 -0400

Thanks Nick.
This will spare me a lot of effort and time. is true I love Stata macros.
It makes life so easier... when one
get used to them.
Have a good night by the way.

                      "Nick Cox"                                                                                                           
                      <[email protected]>           To:       <[email protected]>                                          
                      Sent by:                         cc:                                                                                 
                      owner-statalist@hsphsun2.        Subject:  st: RE: Macro questions.                                                  
                      08/10/2004 06:51 PM                                                                                                  
                      Please respond to                                                                                                    

[email protected]

> I have some questions about macros.
> 1/ I wonder how to count the number of variables in a macro.
> I try the macro extended function (word count) but it doesn't work.
> For example, if I have:
> local myvars "var1 var2 var3 var4"
> then `myvars' contains 4 variables.
> But I do not know how to create a macro that contains the value 4.

I go

. local myvars "var1 var2 var3 var4"

. local nv : word count `myvars'

. di `nv'

and Stata tells me 4. (The macro contains not four
variables, but the names of four variables.)
What are you doing wrong? I have to guess.
Perhaps you are doing something like

. local nv : word count "`myvars'"

. di `nv'

Here you must remember Stata's definition of
a word. Stata by default parses on white space meaning

Amadou loves Stata macros

would be considered by Stata to be 4 words. So far,
so good. But Stata has another rule: double quotes
bind tighter than white space separates, so

"Amadou loves Stata macros"

is just _one_ word. This is just Stata's sense of
a word; forget here what you know about the grammar of
any ordinary language. Now, when you defined

. local myvars "var1 var2 var3 var4"

the " " were just ornamental; Stata took
them as delimiters, not as part of your
definition of the local myvars (and you
could have omitted them). But if you go

. local nv : word count "`myvars'"

the " " are not ornamental at all; they
are working hard to bind what follows
-: word count- into one word.

> 2/ Suppose several datasets containing the same variable floor indexed
> 1, 2, 3, etc.... Floor* number varies with datasets.
> I want to create the macro myfloor
> local myfloor "floor*"
> but then, I do not know how to create the macro containing
> the number of floor
> as in question 1.

This question was asked earlier today. As said then,
the number of variables is not often an issue. Nevertheless,

unab floor : floor*
local nfloor : word count `floor'

> 3/ How to work "inside" macros?
>    suppose you have the following: local "var1 var2 var3 .....varN"
>    suppose later in your dofile you want to do some tables on
> only - say 20-
> variables of  the macro.
>   Is it possible to just choose them (perhaps by specifying
> their position IN
> the macro)
>   without creating other (sub)macros?

The term submacro is yours; I don't know what you mean by it.
There are several ways of doing this, but they usually
involve creating other macros.
> 4/ Finally my last question is how to merge/append several datasets?
>     Suppose you have :
>    local mydata "data1 data2 data3 data4 data5"
>    foreach if i of local mydata {
>        foreach j of local mydata {
>          append `i' using `j'
>       }
>    }
> How to avoid data1 being appended twice?

Various ways. Here's one.

Start with the second word in the macro.

local mydata "data1 data2 data3 data4 data5"
local nsets : word count `mydata'
tokenize `mydata'

use `1'
forval i = 2/`nsets' {
             append using ``i''

*   For searches and help try:

*   For searches and help try:

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