Search code examples
countstatastata-macros

Syntax error when using count in loop


I am trying to run a loop where I count the total in each file under the variable _merge, and then count certain outcomes of _merge, such as _merge=1 and so on. I then want to calculate percentages by dividing each instance of _merge by the total under _merge.

Below is my code:

/*define local list*/
local ward_names B C D E FN FS GS HE


/*loop for each dbase*/
foreach file of local ward_names  {
use "../../../cleaning/sra/output/`file'_ward_CTS_Merged.dta", clear

count if _merge
local ward_count=r(N)

count if _merge==1
local count_master=r(N)

count if _merge==2
local count_using=r(N)

count if _merge==3
local count_match=r(N)

clear 
set obs 1 

g ward_count='ward_count'
g count_master=`count_master'
g count_using=`count_using'
g count_match=`count_match'
g ward= "`file'"

save "../temp/`file'_collapsed_diagnostics.dta", replace

clear

The code was running fine until I tried to add the total count for each ward file:

g ward_count='ward_count'
'ward_count' invalid name 

Is this a syntax error or something more severe?


Solution

  • You need to use ` instead of ' when you refer to a local macro:

    generate ward_count = `ward_count'
    

    EDIT:

    As per @NickCox's recommendation you can improve your code by using the tabulate command with its matcell() option to get the counts all at once:

    tabulate _merge, matcell(A)
    
                     _merge |      Freq.     Percent        Cum.
    ------------------------+-----------------------------------
            master only (1) |          1       16.67       16.67
                matched (3) |          5       83.33      100.00
    ------------------------+-----------------------------------
                      Total |          6      100.00
    
    matrix list A
    
    A[2,1]
        c1
    r1   1
    r2   5
    

    So you could then do the following:

    generate count_master = A[1,1]
    generate count_match = A[2,1]