Announcement

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

  • Table command exiting with error when using user-created program

    I am trying to make a table of normalized differences (based on Imbens and Rubin (2015)) between two definitions of income groups in my data. Here is an example dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(inc_status_1 inc_status_2 weight) byte raedyrs
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1  7696 10
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1 12696  9
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1  7361 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1     0 10
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  6930 12
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1  3364 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    1 1 10986 14
    end
    label values inc_status_1 inc_status_1
    label values inc_status_2 inc_status_1
    label def inc_status_1 1 "Yes", modify
    label values raedyrs EDYRS
    This is my code:

    Code:
    ssc install _gwtmean, replace
    
    cap program drop normdiff
    
    program define normdiff, rclass
    syntax varlist(min=1 max=1)
    cap drop mean1
    cap drop mean1_1
    cap drop m_mean1
    cap drop mean2
    cap drop mean2_1
    cap drop m_mean2
    cap drop std1
    cap drop std2
    cap drop std1sq
    cap drop std2sq
    cap drop ndiff
    cap drop std
    cap drop m_std1
    cap drop m_std2
    
    qui egen double  mean1 = wtmean(`1'), weight(weight) by(inc_status_1)
    qui gen mean1_1 = mean1 if inc_status_1==1
    qui egen m_mean1 = max(mean1_1)
    
    qui egen double  mean2 = wtmean(`1'), weight(weight) by(inc_status_2)
    qui gen mean2_1 = mean2 if inc_status_2==1
    qui egen m_mean2 = max(mean2_1)
    
    qui egen std1=sd(`1') if inc_status_1==1
    qui gen std1sq=std1^2
    qui egen m_std1=max(std1sq)
    qui egen std2=sd(`1') if inc_status_2==1
    qui gen std2sq=std2^2
    qui egen m_std2=max(std2sq)
    qui gen std = sqrt((m_std1+m_std2)/2)
    
    qui gen ndiff=(m_mean1-m_mean2)/std
    format ndiff %20.10f
    qui sum ndiff
    local normdiff = r(mean)
    return local normdiff `normdiff'
    dis "The normalized difference is" %20.10f ndiff
    end
    
    normdiff raedyrs
    
    table (command) (result), ///
            command(Diff = ( r(normdiff)) : ///
                    normdiff raedyrs)
    I am using Stata 17. The program works fine but I get an error in the table command saying "command normdiff exited with an error when run using the entire sample". I am unable to figure out what is causing the error.
    Last edited by Chirag Yadav; 30 Jul 2023, 12:39.

  • #2
    If anyone has any suggestions about tabulating stored macros from the program into a table, that would also help. I want to run "normdiff" on several variables and tabulate it together.

    Comment


    • #3
      Evidently it is because -table- expects the command to support if and in in the -syntax- statement of -normdiff-. This is a good idea anyway to learn to use those qualifiers in your custom program. You can read more in -help syntax- on how to specify and use if and in.

      Comment


      • #4
        Thanks, Leonardo! I managed to get a table using collect instead. This is my code:
        Code:
        collect clear
        quietly: collect , tag(Col[1]): normdiff raedyrs
        collect layout (cmdset) (Col)  
        collect style cell Col[r(normdiff)], nformat(%8.2f)
        collect preview
        I almost have what I want. But I am doing something wrong with the format for the decimal places. Any suggestions on fixing this code?

        Comment


        • #5
          I'm not too familiar with the collect system, but here's what I did. First, you have to return numeric values as scalars. Return them as local macros means that -collect- treats them as strings, and as such, numeric formatting is irrelevant.

          Change your return line in your program to

          Code:
          return scalar normdiff = `normdiff'
          Then this code produces your table, though I'm sure there's probably a better way to do this part.

          Code:
          collect clear
          collect get r(normdiff) : normdiff raedyrs
          collect layout (cmdset) (result)
          collect style cell result[normdiff], nformat(%8.2f)
          collect preview

          Comment


          • #6
            That worked perfectly. Thank you!

            Comment

            Working...
            X