Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referring to local macros using unab

    Hello statalisters,

    I have 227 variables that are grouped by common prefixes. I am trying to use the unab command to create lists of each variable group by prefix, per this forum post. I will then use these prefix-specific lists to create new variables later on.

    I receive the following error: invalid syntax r(198);
    I believe my in-loop reference to the local macro, prefixes, containing the list of prefixes is somehow causing the syntax error. My code is below, followed by some of the output using set trace on.

    Trying the different quote formats in the linked post, including compound quotes, I've had no luck resolving the error. If anybody could point out the syntax error it would be much appreciated!

    Code:
    *local prefixes "ad_3n" "ad_county" "ad_mh" "ad_offcat" "ad_secondstrike" "ad_senttype" "cust_age" "cust_cob" "cust_ethnicity" "cust_genderage" "cust_housing" "cust_mh" "cust_offcat" "cust_risk" "cust_secondstrike" "cust_senttype" "cust_servio" "cust_sexreg" "par_3n" "par_age" "par_atlarge" "par_caseload" "par_ethnicity" "par_mh" "par_offcat" "par_region" "par_secondstrike" "par_senttype" "par_servio" "par_sexreg" "rel_length" "rel_reltype" "rel_senttype" "rel_senttype_length" "rel_sexreg"
    di "`prefixes'"
    
    foreach prefix of local prefixes {
        unab `prefix'_list : `prefix'*
        di "`prefix'_list"
    }
    
    **** FROM SET TRACE ON ****
    
    - foreach prefix of local prefixes {
    - unab `prefix'_list : `prefix'*
    = unab ad_3n"_list : ad_3n"*
      --------------------------------------------------------------------------------------------------------------------- begin unab ---
      - version 6
      - gettoken user 0: 0, parse(" :")
      - gettoken colon 0: 0, parse(" :")
      - if `"`colon'"' != ":" { error 198 }
      = if `""' != ":" { error 198 }
    invalid syntax
      ----------------------------------------------------------------------------------------------------------------------- end unab ---
      di "`prefix'_list"
      }
    r(198);


    Last edited by Thomas Sloan; 14 Dec 2022, 16:46.

  • #2
    One fix that should work is to get rid of the double quotes surrounding each prefix. There's no need for them and you are getting bitten by Stata always stripping the outermost pair of quotes from the entire macro, rather than each prefix as you might expect.

    Comment


    • #3
      Let me add another trap you fell into. Consider this example adapted from your code.
      Code:
      . local prefixes "ad_3n" "ad_county" "ad_mh" "ad_offcat" "ad_secondstrike" 
      
      . display "`prefixes'"
      ad_3nad_countyad_mhad_offcatad_secondstrike
      
      . macro list _prefixes
      _prefixes:      ad_3n" "ad_county" "ad_mh" "ad_offcat" "ad_secondstrike
      
      .
      The display command did not report the actual contents of the local macro prefixes - it tries to hard to figure out what you really want. Just like display 3+3 results in 6.

      Here - using macro list - are some examples of how the trap described by Leonardo Guizzetti in post #2 can lead to unintended results.
      Code:
      . local gnxl a b c
      
      . macro list _gnxl
      _gnxl:          a b c
      
      . local gnxl "a b c"
      
      . macro list _gnxl
      _gnxl:          a b c
      
      . local gnxl a "b two" c
      
      . macro list _gnxl
      _gnxl:          a "b two" c
      
      . local gnxl "a one" b "c three"
      
      . macro list _gnxl
      _gnxl:          a one" b "c three
      
      . local gnxl "a one" b c
      invalid syntax
      r(198);
      
      . local gnxl ""a one" b c"
      
      . macro list _gnxl
      _gnxl:          "a one" b c

      Comment


      • #4
        Thank you so much Leonardo and William for your responses, they resolved my issue! Once I removed the double quotes surrounding each prefix, the unab loop runs as expected. And William, thank you as well for these examples and the reminder to use macro list rather than display for cases like this.

        My code now looks like this, for those who may find this in the future:
        Code:
        local prefixes ad_3n ad_county ad_mh ad_offcat ad_2strike ad_senttype cust_age cust_cob cust_ethnicity cust_genderage cust_housing cust_mh cust_offcat cust_risk cust_2strike cust_senttype cust_servio cust_sexreg par_3n par_age par_atlarge par_caseload par_ethnicity par_mh par_offcat par_region par_secondstrike par_senttype par_servio par_sexreg rel_length rel_reltype rel_senttype rel_senttype_length rel_sexreg
        macro list _prefixes
        
        foreach prefix of local prefixes {
            unab `prefix'_list : `prefix'*
            macro list _`prefix'_list
        }
        Last edited by Thomas Sloan; 15 Dec 2022, 12:59.

        Comment

        Working...
        X