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: using macro lists


From   "Bowser, William (IITA)" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: using macro lists
Date   Mon, 18 Feb 2013 10:00:38 +0000

Nick,

Thanks for the correction and the tips.  

William


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 16 February 2013 08:40 PM
To: [email protected]
Subject: Re: st: using macro lists

This code

  foreach v of varlist ``c'list' {
         local inset_`c'`v' : list ``c'`v'' in `labmatch'
         di `v' if `inset_`c'`v'' == 1
 }

should fail because -display- does not support -if-.. What you may have meant is

  foreach v of varlist ``c'list' {
         local inset_`c'`v' : list ``c'`v'' in `labmatch'
         if `inset_`c'`v'' == 1 di `v'
 }

That can be condensed to

 foreach v of varlist ``c'list' {
         if  `: list ``c'`v'' in `labmatch'' di `v'
 }

although I suspect that you want to see the name "`v'" not the value `v' (which will mean `v'[1].

 foreach v of varlist ``c'list' {
         if  `: list ``c'`v'' in `labmatch'' di  "`v'"
 }

You use -ds3- which is one of mine on SSC from 2002. For your purpose It's been superseded by
-findname- (SJ), although it still works. You are asked to explain where user-written programs you refer to come from.

Nick

On Sat, Feb 16, 2013 at 2:09 PM, Bowser, William (IITA) <[email protected]> wrote:

> I am trying to map sets of variables from different data sets with (possibly) different names, but the same labels and attributes. I have developed the following code thus far and encountered a syntax error that I can't resolve.
>
> /*====================================================================
> =====*/ //extract sublists of non-attached variables from each country 
> data set // 
> /*====================================================================
> =======*/
>
>
> local country Ghana Mali
>
> foreach c of local country {
>
>                 qui ds3 if a1 =="`c'", all(missing(X))
> *               di "`c'"
> *               di "`r(varlist)'"
>                 local `c'list `r(varlist)'  // this is the variable list for each country //
> *               di "``c'list'"
>
> }
>
> foreach c of local country {
> local `c'lablist
>
>         foreach v of varlist ``c'list' {
>                 local `c'`v': variable label `v'
>                 local `c'lablist "``c'lablist' "`c'`v'""        //create list of labels in each country //
> *               di "`c'"
> *               di "`v'`c'"
>
>         }
>
> }
>
> /*====================================================================
> =========*/ //Create a list of the intersection of each 
> `country'lablist // 
> /*====================================================================
> =========*/
>
> ***remember to generalize this later *** local labmatch : list 
> Ghanalablist & Malilablist // this works fine ! //
>
> local n : word count `labmatch'
> di "`n'"
>
> foreach c of local country {
>         local `c'labmatch :list `c'lablist & labmatch
>         local n`c' : word count ``c'labmatch'
> }
>
> // verify matches //
> assert `nGhana' == `nMali'
> local same : list Ghanalabmatch==Malilabmatch // check to make sure 
> the lists have the same labels // di "`same'"
>
> I RUN INTO PROBLEMS HERE:
>
> foreach c of local country {
>
>         foreach v of varlist ``c'list' {
>          local inset_`c'`v' : list ``c'`v'' in `labmatch'
>          di `v' if `inset_`c'`v'' == 1
>
>         }
>
> }
>
>
> The above code seems like it should work to me but Stata gives me a syntax error when I run it. Please, any help would be greatly appreciated.
*
*   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