Announcement

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

  • Unknown Function Error

    Hi,

    I started getting an error like this when running my do file in Stata. I have used this code many times before but never got an error. Does anyone have any idea?


    HTML Code:
    preserve
    * + Change the caption & varname
    
    sum `depvar' if sample == `sample2' [aw=dwt]
    local mean`depvar'`sample2' : di %12.2f r(mean)
    sum `depvar' if sample == `sample1' [aw=dwt]
    local mean`depvar'`sample1' : di %12.2f r(mean)
    local mean`depvar'diff : di %12.1f (`mean`depvar'`sample1'' - `mean`depvar'`sample2'') / `mean`depvar'`sample2'' * 100
    di "`var'"
    svy: mean `depvar', over(sample)
    lincom [`depvar']`sample1'- [`depvar']`sample2'
    local y : di %9.0g 2*ttail(r(df),abs(r(estimate)/r(se)))
    local x: di %12.3f `y'
    local p_`depvar' " `mean`depvar'`sample2'' & `mean`depvar'`sample1'' & `mean`depvar'diff' & p < `x' "
    
    
    foreach ivar in `indepvarscat' {
    local j
    levelsof `ivar', local(numlist)
    global count: word count `numlist'
    
    foreach num of numlist `numlist' {
    local var "`ivar'`num'"
    local j = `j' + 1
    
    qui sum `depvar' if `var' == 1 & sample == `sample2' [aw=dwt]
    local mean`depvar'`sample2'`var' : di %12.2f r(mean)
    qui sum `depvar' if `var' == 1 & sample == `sample1' [aw=dwt]
    local mean`depvar'`sample1'`var' : di %12.2f r(mean)
    local mean`depvar'diff : di %12.1f (`mean`depvar'`sample1'`var'' - `mean`depvar'`sample2'`var'') / `mean`depvar'`sample2'`var'' * 100
    
    svy: mean `var', over(sample)
    lincom [`var']`sample1'- [`var']`sample2'
    local y : di %9.0g 2*ttail(r(df),abs(r(estimate)/r(se)))
    
    local x: di %12.3f `y'
    local p_`depvar'_all`ivar'`j' " `mean`depvar'`sample2'`var'' & `mean`depvar'`sample1'`var'' & `mean`depvar'diff' & p < `x' "
    di "`p_`depvar'_all`ivar'`j''"
    }
    }
    
    cap file close myfile
    file open myfile using `texdir'\Tables5_9.tex, write replace
    file write myfile "\begin{table}[!htb]" _n // htbp
    file write myfile "\begin{center}" _n
    file write myfile "\caption{Temporal changes in the dependent variable by population sub-groups, `sample2'-`sample1' (Tables 5 and 9)} " _n
    file write myfile "\label{table: Tables5_9}" _n
    file write myfile "\resizebox{0.85\textwidth}{!}{" _n
    file write myfile " \begin{tabular}{llccd{-1}l} " _n
    file write myfile "\toprule" _n
    file write myfile " \multicolumn{2}{l}{\multirow{2}[3]{*}{Variable}} & \multicolumn{2}{c}{\parbox{2.4cm}{Prevalence of poor health}} & \multicolumn{1}{c}{Change,} & \multirow{2}[3]{*}{Wald $\chi^2$} " _n
    file write myfile " && `sample2' & `sample1' & \multicolumn{1}{c}{\%} & " _n
    file write myfile "\midrule" _n
    global countivc: word count `indepvarscat'
    foreach ivar in `indepvarscat' {
    local ji = `ji' + 1
    local j
    levelsof `ivar', local(numlist)
    global count: word count `numlist'
    foreach num in `numlist' {
    local var "`ivar'`num'"
    local j = `j' + 1
    di "`j'"
    di "`p_`depvar'_all`ivar'`j''"
    if `j' == 1 {
    local varlab "\multirow{$count}{*}{``ivar'lab'}"
    }
    else {
    local varlab
    }
    file write myfile "`varlab' & ``var'lab' & `p_`depvar'_all`ivar'`j'' \\ " _n
    }
    
    if `ji' < $countivc {
    file write myfile "\midrule" _n
    }
    }
    I got this error:


    HTML Code:
    . foreach ivar in `indepvarscat'  {
      2.         local j
      3.         levelsof `ivar', local(numlist)
      4.         global count: word count `numlist'  
      5.
    .         foreach num of numlist `numlist'  {
      6.                 local var "`ivar'`num'"
      7.                 local j = `j' + 1
      8.                
    .                 qui sum `depvar' if `var' == 1 & sample == `sample2' [aw=dwt]
      9.                 local mean`depvar'`sample2'`var' : di %12.2f r(mean)
     10.                 qui sum `depvar' if `var' == 1 & sample == `sample1' [aw=dwt]
     11.                 local mean`depvar'`sample1'`var' : di %12.2f r(mean)
     12.                 local mean`depvar'diff : di %12.1f (`mean`depvar'`sample1'`var'' - `mean`depvar'`sample2'`var'') / `mean`depvar'`sample2'`var'' * 100
     13.                
    .         svy: mean `var', over(sample)
     14.         lincom [`var']`sample1'- [`var']`sample2'
     15.                 local y : di  %9.0g  2*ttail(r(df),abs(r(estimate)/r(se)))
     16.
    .                 local x: di %12.3f `y'
     17.                 local p_`depvar'_all`ivar'`j' " `mean`depvar'`sample2'`var'' & `mean`depvar'`sample1'`var'' & `mean`depvar'diff' & p < `x' "
     18.                 di "`p_`depvar'_all`ivar'`j''"
     19.         }
     20. }
    -1 1 2 3 4 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    unknown function 1:di%12.2fr()
    r(133);
    
    end of do-file
    
    r(133);
    Last edited by Marsie Wass; 21 Aug 2023, 09:45.

  • #2
    This is very strange. The error must be coming from line 9 or line 11, but why are the whitespace characters being ignored?

    Code:
    local mean`depvar'`sample2'`var' : di %12.2f r(mean)
    The end of the line is interpreted as

    Code:
    1:di%12.2fr(mean)
    Is it possible these whitespace characters are tabs rather than spaces? Can you please go through these two lines and delete each space, then add a space character back in, then post back about whether you get the same error?

    Edit: this kind of weirdness with characters can happen if you copy this code between different text editors, or (infrequently) if you move the file between operating systems. Do you recall doing something like that at some point?
    Last edited by Daniel Schaefer; 21 Aug 2023, 11:20.

    Comment


    • #3
      Originally posted by Daniel Schaefer View Post
      This is very strange. The error must be coming from line 9 or line 11, but why are the whitespace characters being ignored?

      Code:
      local mean`depvar'`sample2'`var' : di %12.2f r(mean)
      The end of the line is interpreted as

      Code:
      1:di%12.2fr(mean)
      Is it possible these whitespace characters are tabs rather than spaces? Can you please go through these two lines and delete each space, then add a space character back in, then post back about whether you get the same error?

      Edit: this kind of weirdness with characters can happen if you copy this code between different text editors, or (infrequently) if you move the file between operating systems. Do you recall doing something like that at some point?
      Hi,thank you so much. I tried your suggestion but unfotunately I got the same error. It's so stragne I've never seen this one before, but my last 2 days I keep getting this error.

      Comment


      • #4
        Hi Marsie,

        This small block of code is a simplification of your problem. Notice that there is a space at the end of "four " in the outer loop.

        Code:
        clear
        set obs 100
        gen randnum = rnormal()
        foreach ivar in "one" "two" "three" "four "{
            di "`ivar'"
            foreach num of numlist 1 2 3 4{
                local var = "`ivar'`num'"
                qui sum randnum
                local mean`var' : di %12.2f r(mean)
                local mean_diff : di %12.2f (`mean`var'' - `mean`var'')/`mean`var'' * 100
            }
        }
        If you run this code, it will rase the same error on the last value of ivar because of the space. If you remove the space, the loop runs without error.

        Code:
        . foreach ivar in "one" "two" "three" "four "{
          2.         di "`ivar'"
          3.         foreach num of numlist 1 2 3 4{
          4.                 local var = "`ivar'`num'"
          5.                 qui sum randnum
          6.                 local mean`var' : di %12.2f r(mean)
          7.                 local mean_diff : di %12.1f (`mean`var'' - `mean`var'')/`mean`var'' * 100
          8.         }
          9. }
        one
        two
        three
        four
        unknown function 1:di%12.2fr()
        This tells me that there must be a level of one of your indepvarscat variables that has a space character, either at the end, or as the entire level. The space character in the variable level is the source of the error.
        Last edited by Daniel Schaefer; 22 Aug 2023, 10:52.

        Comment


        • #5
          Hi Daniel,

          Thank you so much for your help. I've just checked my variables for blocking one by one. And found one of them has a space character just like you said. Now I'm gonna delete and go for the analysis. Thank you very much.

          Comment

          Working...
          X