Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: String vector


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   RE: st: String vector
Date   Tue, 01 May 2007 10:27:07 +0900

Hendri Adriaens wrote:

Hi again,

Another one that I do not understand:

 forvalues x=1/2 {;
   if `x'==1 {
     local bt="bt_m";
     local gt="gt_m";
   };
   if `x'==2 {
     local bt="bt_v";
     local gt="gt_v";
   };
   forvalues y=1/20 {;
     if `y'==20 {;
       local groep="b_lft95_"+"`gesl'";
     };
     if `y'!=20 {;
       local groep="b_lft"+string((`y'-1)*5)+"_"+string(`y'*5)+"`gesl'";
     };
     replace `groep'=`groep'*`bt'/`gt';
   };

Why does this complain like:
/gt_m invalid name

I guess it thinks the backslash is part of the string. Can I do something
about that? Notice that it doesn't seem to think this with the *`bt'...

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

It looks more like `bt' is undefined.  You can diagnose whether this is the
case by asking Stata to report what's in the bt local macro just before the
offending line:

display in smcl as input "local bt = `bt'"
replace `groep' = `groep' * `bt' / `gt'

There should be something after the equal sign in the diagnostic probe's
display line of code.

As an alternative, you can

assert "`bt'" != ""
replace `groep' = `groep' * `bt' / `gt'

The diagnostic probe will force an error if the local is undefined.

Joseph Coveney

P.S. What you seem to be trying to accomplish is normally done in Stata in a
completely different approach (i.e., in a "Stata-ish" programming style that
would result in much more efficient, readable and maintainable code).  The
appearance of your code snippets over the past few posts looks like the
approach a SAS programmer coming to Stata would attempt.  I don't know if
that's the case, but if it is, then I recommend a NetCourse from StataCorp
in Stata programming in order to break out of the SAS mindset that's causing
you so much of your troubles.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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