Comma passed with varlist variable in ado program

I have a short .ado file to standardize string variables from multiple sources. The program takes one string variable, renames it with a suffix, then replaces the original variable with a standardized name.

But syntaxit does not handle the comma correctly after the string variable. That is, instead of transmitting, countryit transmits country,and gives an error.

Is space between varnameand required ,? Or do I have a misunderstanding of how I should use syntaxand varlist?

clear
set obs 10
generate country = "United States of America"

* runs fine without `suffix()` options
country_names country
list

/*
. list

     +------------------------------------------+
     |                country_0         country |
     |------------------------------------------|
  1. | United States of America   United States |
  2. | United States of America   United States |
  3. | United States of America   United States |
  4. | United States of America   United States |
  5. | United States of America   United States |
     |------------------------------------------|
  6. | United States of America   United States |
  7. | United States of America   United States |
  8. | United States of America   United States |
  9. | United States of America   United States |
 10. | United States of America   United States |
     +------------------------------------------+
*/

* but gives error with `suffix()` 
country_names country, suffix(_orig)

/*
. country_names country, suffix(_orig)
, invalid name
r(198);
*/

* `set trace on` reveals comma passed as part of `varlist`
set trace on
country_names country, suffix(_orig)

/*
. country_names country, suffix(_orig)
  ---------------------------------------------------------------------------------------------- begin country_names ---
  - version 11.2
  - syntax varname(string) [, Suffix(string) ]
  - quietly {
  - if "`suffix'" == "" local suffix "_0"
  = if "_orig" == "" local suffix "_0"
  - rename `1' `1'`suffix'
  = rename country, country,_orig
, invalid name
    generate `1' = proper(`1'`suffix')
    replace `1' = "United States" if inlist(`1', "United States Of America")
    local name: variable label `1'`suffix'
    label variable `1' "`name'"
    label variable `1'`suffix' "`name' (orig)"
    }
  ------------------------------------------------------------------------------------------------ end country_names ---
r(198);
*/

* if I leave space before comma, then program works
country_names country , suffix(_orig)
list

/*
. list

     +----------------------------------------------------------+
     |                country_0    country_orig         country |
     |----------------------------------------------------------|
  1. | United States of America   United States   United States |
  2. | United States of America   United States   United States |
  3. | United States of America   United States   United States |
  4. | United States of America   United States   United States |
  5. | United States of America   United States   United States |
     |----------------------------------------------------------|
  6. | United States of America   United States   United States |
  7. | United States of America   United States   United States |
  8. | United States of America   United States   United States |
  9. | United States of America   United States   United States |
 10. | United States of America   United States   United States |
     +----------------------------------------------------------+
*/

Here is the .ado file.

*! 0.1 Richard Herron 2/11/2014

/* use to standardize country names across several data sources */

program country_names
    version 11.2
    syntax varname(string) [, Suffix(string) ]

    quietly {

        /* default suffix */
        if "`suffix'" == "" local suffix "_0"

        /* save original as new variable w/ suffix */
        rename `1' `1'`suffix'

        /* first standardize capitalization */
        generate `1' = proper(`1'`suffix')

        /* -if- picks bad names from several sources */
        replace `1' = "United States" ///
            if inlist(`1', "United States Of America")

        /* fix labels */ 
        local name: variable label `1'`suffix'
        label variable `1' "`name'"
        label variable `1'`suffix' "`name' (orig)"

    }

    end
+3
source share
1 answer

syntax varlist , .

, .

1. syntax, 0 , 1, 2 .. , .. "", .

, , , Stata . 1 country, ( ) ( , suffix _orig)

rename `1' `1'`suffix'

rename country, country,_orig 

set trace on, , , . rename, country ( ) - , .

: 1 , varlist.

. syntax varname, , , - varlist.

. , , .

: syntax .

+3

All Articles