Announcement

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

  • forvalues with counter

    Hello,

    I am a PhD student and I have not been using Stata that long (about a year) so my apologies if the answer to this question is obvious. I have been looking for an answer for why this is happening for several days but have not had any success on my own.

    Recently, my faculty advisor recommended that I look at some national data from the National Survey on Drug Use and Health (NSDUH) as part of my dissertation work. He is out of the country and this is quite time sensitive. After completing my analyses, I was going through the data documentation regarding estimate suppression for potentially unreliable estimates. They include code for SUDAAN, Stata and SAS. I tried the suggested Stata code (see below) and continually get the error message 'counter invalid name' (see bolded part of code where things are going wrong for me).

    Here is a link to the NSDUH documentation (see page 71): https://www.samhsa.gov/data/sites/de...erence2015.pdf

    Here is the code provided producing the error message 'counter' invalid name:

    use using “.\\dataname.dta”, clear /*Ensure all variables are lower case*/
    rename *, lower /*ID Nesting variables (VESTR and VEREP) and weight variable (ANALWT - standard single-year, person-level analysis weight*/

    svyset verep [pw=analwt], strata(vestr) dof(750)

    gen total_out=.
    gen setotal=.
    gen mean_out=.
    gen semean=.
    gen nsum=.
    gen wsum=.
    gen deffmean=.

    /* Estimated means of past month alcohol use by year and gender*/
    /*Year variable, where 2013=1 & 2014=2*/
    /*Gender variable, where male=1 & female=2*/

    svy: mean alcmon, over(year irsex)

    matrix M=e(b) /*Store mean estimates in matrix M*/
    matrix S=e(V) /*Store variances in matrix S*/
    matrix N=e(_N) /*Store sample size in matrix N*/
    matrix W=e(_N_subp) /*Store weighted sample size in matrix W*/
    estat effects, deff srssubpop /*Obtain design effect*/
    matrix D=e(deff) /*Store design effect in matrix D*/

    /*Extract values stored in the M, S, N, W, and D matrices defined above to the mean_out, semean, nsum, wsum, and deffmean variables. The loop ensures that the appropriate values are extracted for each value of year and gender.*/

    local counter=1
    forvalues i=1/2 { /*number of years*/
    forvalues j=1/2 { /* number of gender categories*/
    replace mean_out=(M[1,`counter’]) if year==`i’ & irsex==`j’
    replace semean=(sqrt(S[`counter’,`counter’])) if year==`i’ & irsex==`j’
    replace nsum=(N[1,`counter’]) if year==`i’ & irsex==`j’
    replace wsum=(W[1,`counter’]) if year==`i’ & irsex==`j’
    replace deffmean=(D[1,`counter’]) if year==`i’ & irsex==`j’
    local counter=`counter’+1
    }
    }


    Thank you in advance for any help or suggestions you can provide.

  • #2
    Welcome to Statalist.

    I am sorry to say that the document from which you copied the code that you pasted into Stata suffers from a typographical problem.

    To correct your problem, wherever your code has something that looks like
    Code:
    `counter´
    you need instead to have
    Code:
    `counter'
    where the first character is (on an American English keyboard) the so-called "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual so-called "single quote" ("apostrophe") just to the left of the RETURN key.

    It appears to me that you currently have "left single quotes" where they are required, but the apostrophe is instead an acute accent character.

    And a word of warning - the Stata PDF documentation suffers from a similar problem.

    PS - for the typographically inclined, the wrong version uses the unicode grave accent and acute accent, while the correct version uses the unicode grave accent and apostrophe.

    Comment


    • #3
      Stata quotes get screwed up all the time in publications. If you ever get something published you better check that so-called smart quotes didn't replace the quote marks that were supposed to be there. Also beware of smart quotes if you are typing something directly into Word.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        I have no idea how long that would have took me to see. The correction worked perfectly. Thank you very much!

        Comment


        • #5
          I have one more question. After running the forvalue loop successfully several times, it all of a sudden stopped working and giving me a 'invalid syntax r(198)' error. It worked for several variables but just stopped working despite my not changing anything in the code. Any ideas why it would stop working? Is STATA having a meltdown?

          From results window:

          . local counter=1

          . forvalues i=1/4 {
          2. forvalues j=1/2 {
          3. forvalues k=1/5 {
          4. replace mean_out=(M[1,`counter']) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
          5. replace semean=(sqrt(S[`counter',`counter1'])) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
          6. replace nsum=(N[1,`counter']) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
          7. replace wsum=(W[1,`counter']) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
          8. replace deffmean=(D[1,`counter']) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
          9. local counter=`counter'+1
          10. }
          11. }
          12. }
          (5,987 real changes made)
          invalid syntax
          r(198);

          Also, when I reran it for outcome variables that already gave me results, it won't work now and gives me the same error. I'm not sure why it would work once then not the second time. Thank you again, Sam

          Comment


          • #6
            Stata is not having a meltdown. Your code has broken down.

            The first replace command worked and reported the number of changes it made. So let's look at the second replace command.
            Code:
            replace semean=(sqrt(S[`counter',`counter1'])) if RACE==`i' & IRSEX==`j' & CATAG3==`k'
            What is the local macro counter1? You do not show it in your code. Comparing it to the code you presented in post #1, it appears you introduced a typographical error.

            Note also that the name of the program you are using is typed "Stata" rather than "STATA", although I will agree that the trademark displayed at the start of the Results window has an upper case appearance at a casual glance: only the letters "a" are obviously lower case
            Code:
              ___  ____  ____  ____  ____ (R)
             /__    /   ____/   /   ____/
            ___/   /   /___/   /   /___/   15.1

            Comment


            • #7
              Not sure how I snuck a 1 in there but good eye! Thank you

              Comment

              Working...
              X