Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Macros in -listtab- header: syntax error


From   Friedrich Huebler <fhuebler@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Macros in -listtab- header: syntax error
Date   Thu, 22 Nov 2012 12:26:01 -0500

I use Stata 11.2 with Windows 7 and would like to create a
tab-delimited text file similar to the one below.

Level A                    Level B           Sum
Grade 1  Grade 2  Grade 3  Grade 1  Grade 2
24.7     24.7     24.7     17.6     17.6     109.3
26.2     26.2     26.2     23.4     23.4     125.5

For this purpose I use the -listtab- Stata add-on by Roger Newson (see
-ssc d listtab-). The data is organized by level and grade, and the
number of grades per level varies. To automate the creation of the
table header I would like to use macros. When I include the macros in
the -listtab- command, Stata aborts with an "invalid syntax" error. I
don't know if this is due to the way I create the macros (e.g. the
combination of single and double quotes) or if this is a problem with
-listtab-.

The problem can be demonstrated with the commands below. Step 1
creates a meaningless test dataset that mimics the structure of my
original data. Step 2 creates a tab-delimited text file in the desired
format; here, the table header is specified manually in the -listtab-
command. Step 3 is an attempt to automate the creation of the second
row in the table header; the number of grades per level is unknown but
is not greater than 8, hence the loop over the numbers 1 to 8. Step 4
is the -listtab- command that fails with a syntax error.

Please note that the macros "agrades" and "bgrades", shown with the
-di- command at the end of Step 3, contain the same text as the table
header in Step 2:

Grade 1`t'Grade 2`t'Grade 3`t'Grade 1`t'Grade 2`t'

-set trace on- reveals that the error occurs after the following line
but I don't know how to interpret this information:

  = file write __00000B `"Grade 1`"' _n
invalid syntax
r(198);

Thank you for your help,

Friedrich


* Step 1: Test dataset
sysuse auto, clear
forval i = 1/3 {
  gen a`i' = mpg if trunk<13
}
forval i = 4/5 {
  gen b`i' = mpg if trunk>=13
}
collapse a1 - b5, by(foreign)
egen sum = rowtotal(a1 - b5)
format a1 - sum %5.1f

* Step 2: -listtab- table without syntax error
* Tab character for -listtab- header
local t = char(9)
* Tab-delimited text file
#delimit ;
listtab a1 - sum using "table1.txt",
  head("Level A`t'`t'`t'Level B`t'`t'Sum"
       "Grade 1`t'Grade 2`t'Grade 3`t'Grade 1`t'Grade 2`t'")
  rstyle(tabdelim) replace
;
#delimit cr

* Step 3: Create entries for a and b variables in table header
* Entry for a variables
local agrades ""
local j = 1
forval i = 1/8 {
  capture confirm variable a`i'
  if !_rc {
    local agrades "`agrades'Grade `j'`""t""'"
    local j = `j' + 1
  }
}
* Entry for b variables
local bgrades ""
local j = 1
forval i = 1/8 {
  capture confirm variable b`i'
  if !_rc {
    local bgrades "`bgrades'Grade `j'`""t""'"
    local j = `j' + 1
  }
}
* Show macros with table header entries
di "`agrades'`bgrades'"

* Step 4: -listtab- table with syntax error
* Tab character for -listtab- header
local t = char(9)
* Tab-delimited text file
#delimit ;
listtab a1 - sum using "table2.txt",
  head("Level A`t'`t'`t'Level B`t'`t'Sum"
       "`agrades'`bgrades'")
  rstyle(tabdelim) replace
;
#delimit cr
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index