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]

st: Check if variable names are equal for program flow


From   Eric Canen <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Check if variable names are equal for program flow
Date   Tue, 10 Sep 2013 10:31:48 -0700 (PDT)

I want to loop over a set of variables using a local macro. There is a variable in the set where I want to do additional 
procedures when it comes up in the loop, so I added an if statement that is meant to compare whether or not
the current variable is the one that has additional procedures. The problem is that the if statement is not evaluating
as I expected. Its evaluating as true even when as far as I can tell it should be false. Here is the code:

local cig_init smokever_initiation smokwholcig smokwholage18_2cat smokwholage18_24

foreach svyitem in `cig_init' {

//do some stuff for every variable
//added the next command to check what the if is evaluating to
display `svyitem' == smokwholage18_2cat
if `svyitem' == smokwholage18_2cat{
//do some extra stuff on smokwholage18_2cat
{
}

The first variable in the loop (smoker_initiation) works fine. The debugging code using the display command prints a "0" like I expected.
The second variable (smokwholcig) seems to have a problem. The debugging code prints a "1" and so it is evaluating to true when 
as far as I can tell it should be evaluating to a "0" because as far as I can tell smokwholcig == smokwholage18_2cat is false.

I also tried evaluating the variable names as strings and it just concatenates them together. Specifically I ran:


. display "`svyitem'" == "smokwholage18_2cat" 

smokwholcigsmokwholage18_2cat


It just concatenated the two strings together.

I have run the frequencies on both variables and they are different from one another though because of skip patterns in the survey
the cross tabulation does yield a reduced table (see below). That is part of the reason I want to use the if statement to control
what is done for smokwholage18_2cat. Here is the output of the cross tabulation between the two variables:

. tabulate smokwholcig smokwholage18_2cat

  Have you |

      ever | How old were you when you smoked
  smoked a | a whole cigarette for the first
     whole |         time before 18?
cigarette? |         1          2        997 |     Total
-----------+---------------------------------+----------
         1 |       118        139          7 |       264 
-----------+---------------------------------+----------
     Total |       118        139          7 |       264 

Can anyone help me figure out why this if statement is working the way it is and what I can do to fix it?

*
*   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