Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • levelsof and local macros

    Hi everyone,

    I'm trying to run a set of regressions for the levels of a string variable but I'm getting an invalid syntax error message.
    Assume the variable with the classification information is "country", which has Argentina, Brazil, Chile, United States and so on.
    I need to run the following code:

    levelsof country, local(cnt)
    gen error=.
    foreach c of local cnt{
    reg Y X1 X2 X3 if country==`c'
    predict res, residuals
    replace error=res if e(sample)
    drop res
    }

    As I say, it returns the message invalid syntax. I've tried putting `c' as "`c'" but still get an error message where the countries are not found. It also seems to have problems dealing with longer names like United States which is comprised by two names (in this case I tried "`c'"). It must be something simple, but I'm unable to get a solution.

    Any ideas on how to solve this?

  • #2
    There are perhaps a couple of different issues here. One of them is that -if country == `c'- will definitely not work with `c' taking on the values of strings. To be completely safe, it should be -if country == `"`c'"'-.

    But it may also be that due to missing values of Y, X1, X2, and X3, there will be some countries for which there are no observations, or too few observations, to carry out the regression. Remember that if any of the variables Y, X1, X2, or X3 has a missing value, the entire observation is excluded from the estimation sample. So it doesn't take a large amount of missing values scattered through the data set to end up with an occasional country that has no usable observations at all.

    Here's some made-up data that might look something like yours, and some code that runs with it and does what you're trying to do.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str13 country float(Y X1 X2 X3)
    "Albania"        .11216265   .4056266    .11855792    .881642
    "Albania"         .9487334   .6343603    .25556746   .8008212
    "Albania"                .   .4043443            .   .5523863
    "Albania"        .18564783   .3321862     .4825758  .02413011
    "Albania"         .9472316   .5508492     .4706297          .
    "Albania"        .08942585  .12353604     .0935955          .
    "Albania"                .  .10792727     .7919037   .5611674
    "Albania"         .8825376   .8705296            .          .
    "Albania"         .9484983   .4575412    .09931891   .9865241
    "Albania"         .7505445   .4398758    .02711774          .
    "France"         .21017867   .6809552      .372983    .941715
    "France"        .035859343  .15291964     .5142699   .3550118
    "France"          .9874877   .8800637 .00028821192   .9601552
    "France"          .9763448   .7559011       .06659   .5525427
    "France"         .20113812   .5950673     .8159919   .7296131
    "France"          .6616006          .     .6078497          .
    "France"                 .   .9501313     .8575507  .10575833
    "France"         .05222337   .4874784    .02750407   .8128985
    "France"          .7655026          .     .8894401  .47012335
    "France"                 .   .9149532     .3910225  .13379718
    "Germany"         .8969765   .3617445    .12547621   .3405329
    "Germany"         .7800556          .     .6922931          .
    "Germany"         .6283849   .8399968   .003448891          .
    "Germany"         .9743183   .7211111            .  .53831494
    "Germany"        .08119465          .            .          .
    "Germany"                .   .2747885            .   .9204913
    "Germany"                .  .17099117     .6826897   .9224355
    "Germany"       .019561933   .3256108     .8822002          .
    "Germany"         .2985383   .9440667            .   .1770748
    "Germany"        .20679154   .6692844     .5584789   .4117713
    "United States"          .   .4353405    .10340178          .
    "United States"   .7333733 .021285385            .   .4829012
    "United States"    .399902   .9941382     .8328871   .8358622
    "United States"          .   .7446451            .          .
    "United States"    .574749   .7595631    .13414234   .7862822
    "United States"   .9457484          .     .6700398   .4184419
    "United States"  .53569984          .     .4374269  .23491123
    "United States"   .6551434   .5321585     .4723527 .004203058
    "United States"   .8314545   .0918318     .5459082  .53706163
    "United States" .015391795  .14427297     .8599574   .6231912
    end
    
    
    gen error = .
    levelsof country, local(cnt)
    foreach c of local cnt {
        capture noisily regress Y X1 X2 X3 if country == `"`c'"'
        if c(rc) == 0 { // SUCCESSFUL REGRESSION
            predict res, residuals
            replace error = res if e(sample)
            drop res
        }
        else if inlist(c(rc), 2000, 2001) { // NOT ENOUGH OBSERVATIONS
            continue
        }
        else {
            display as error `"Unexpected problem: country  == `c'"'
            exit c(rc)
        }
    }
    Now, it may be that this won't work in your data, if it differs in some material way from what I made up. Since you don't show an example of your data, anyone who wants to help you is left to his or her imagination about it. So perhaps we have now both wasted our time.

    In the future, whenever you want help with code, show an example of your data. The more you help those who want to help you to understand what you are working with, the better the chance that they will be able to respond with code that actually works for you. The best way to show example data is with the -dataex- command, as I have done here. If you are running Stata version 15.1 or a completely updated 14.2, you already have it in official Stata. If not, run -ssc install dataex-, to get it. Either way, run -help dataex- to see the simple instructions for use.

    Remember: if you want help with code, always post example data. When you post example data, always use -dataex-.

    Comment


    • #3
      No obvious errors with your code. I would show the details of output.

      Comment


      • #4
        Thanks all! it seems to be working!!

        Comment

        Working...
        X