Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Macros in -listtab- header: syntax error


From   Friedrich Huebler <[email protected]>
To   [email protected]
Subject   Re: st: Macros in -listtab- header: syntax error
Date   Thu, 22 Nov 2012 16:14:09 -0500

Roger,

I see now that the line that you mentioned may be interpreted by Stata
as containing an unmatched ` character.

  local bgrades "`bgrades'Grade `j'`""t""'"

With this command I am trying to combine three separate elements
enclosed in double quotes. The first of those elements ends in a left
single quote (`):

  "`bgrades'Grade `j'`" + "t" + "'"

At the core the problem is that I am trying to create a macro with a
string that includes `t', text that should appear in the string as
such (a left single quote, then a lowercase t, then a right single
quote) instead of being interpreted as a macro. I thought I had
achieved this goal because the content of macro "agrades" is:

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

and the content of macro "bgrades" is:

  Grade 1`t'Grade 2`t'

The two macros combined appear as:

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

The line above is exactly what I need in the -listtab- command and
there doesn't appear to be an unmatched ` character. For this reason I
am puzzled by the syntax error. As an alternative I tried the line
below, with the left single quote now enclosed in double quotes:

  local bgrades "`bgrades'Grade `j'""`""t""'"

The combined macros look correct:

. di "`agrades'`bgrades'"
Grade 1`t'Grade 2`t'Grade 3`t'Grade 1`t'Grade 2`t'

However, -listtab- still stops with a syntax error.

  = file write __00000B `"`"' _n
invalid syntax

Friedrich


On Thu, Nov 22, 2012 at 2:19 PM, Roger B. Newson
<[email protected]> wrote:
> Your quoted line
>
>
> file write __00000B `"Grade 1`"' _n
>
> appears (to me) to contain an unmatched ` character, immediately following
> the string "Grade 1". This may have originated from your line of code (in
> Step 3)
>
>
>      local bgrades "`bgrades'Grade `j'`""t""'"
>
> which seems to me to contain an unmatched ` character just after `j' and
> before "". I would expect this to cause some confusion somewhere, especially
> as your unmatched ` is just before a ", and the pair of characters looks
> like a left compound quote.
>
> I hope this helps.
>
> Best wishes
>
> Roger
>
> Roger B Newson BSc MSc DPhil
> Lecturer in Medical Statistics
> Respiratory Epidemiology and Public Health Group
> National Heart and Lung Institute
> Imperial College London
> Royal Brompton Campus
> Room 33, Emmanuel Kaye Building
> 1B Manresa Road
> London SW3 6LR
> UNITED KINGDOM
> Tel: +44 (0)20 7352 8121 ext 3381
> Fax: +44 (0)20 7351 8322
> Email: [email protected]
> Web page: http://www.imperial.ac.uk/nhli/r.newson/
> Departmental Web page:
> http://www1.imperial.ac.uk/medicine/about/divisions/nhli/respiration/popgenetics/reph/
>
> Opinions expressed are those of the author, not of the institution.
>
>
> On 22/11/2012 17:26, Friedrich Huebler wrote:
>>
>> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index