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   Fri, 23 Nov 2012 12:14:18 -0500

Roger,

Thank you, this is perfect. The -headchar()- option solves my problem.
Below is my original example, modified to show automatic generation of
the table headers that identify levels and grades in the data.

Many thanks for -listtab- and for the continued updates to this package.

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: Automatic creation of values for -headchar()- option
* Headers for "a" variables
local j = 1
forval i = 1/8 {
  capture confirm variable a`i'
  if !_rc {
    if `j' == 1 {
      char a`i'[levels] "Level A"
    }
    char a`i'[grades] "Grade `j'"
    local j = `j' + 1
  }
}
* Headers for "b" variables
local j = 1
forval i = 1/8 {
  capture confirm variable b`i'
  if !_rc {
    if `j' == 1 {
      char b`i'[levels] "Level B"
    }
    char b`i'[grades] "Grade `j'"
    local j = `j' + 1
  }
}
* Header for "sum" variable
char sum[levels] "Sum"

* Step 3: -listtab- table (tab-delimited text file)
#delimit ;
listtab a1 - sum using "table.txt",
  headchar(levels grades)
  rstyle(tabdelim)
  replace
;
#delimit cr


On Thu, Nov 22, 2012 at 4:52 PM, Roger B. Newson
<[email protected]> wrote:
> What you are doing doesn't seem to produce a well-formed string argument for
> input to -file-.
>
> A better idea might be to use the -headchar()- option of -listtab-, after
> storing the header strings "Grade 1", "Grade 2" and "Grade 3" in a named
> variable characteristic for each of the variables to be output (eg
> -headchar(varname)- if we want each variable to have a characteristic with
> name -varname-). This will produce header row containing the specified
> characteristic values of the output variables, separated by the delimiter,
> which you have specified to be a tab. The -headchar()- and -footchar()-
> options were added in a recent -listtab- update on SSC.
>
> for instance, you might type
>
> char a1[varname] "Grade 1";
> char a2[varname] "Grade 2";
> char a3[varname] "Grade 3";
> listtab a1 a2 a3 using mytab.txt, rstyle(tabdelim) headchar(varname)
> replace;
>
> This should create a header row containing the headers "Grade 1", "Grade 2"
> and "Grade 3" for variables a1, a2 and a3, respectively. You can, of course,
> name multiple characheristics in -headchar()- to make multiple header rows.
>
>
> 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 21:14, Friedrich Huebler wrote:
>>
>> 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