Announcement

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

  • getting a local to work with quoted string values

    Hi All,

    I am trying to create a local that will ultimately serve as the "if" qualifier for a tabulation, etc... The issue I am hitting up against is how to get the local to put quotes around the string values I am specifying in the local? Here is some example code to show what I mean:

    Code:
    clear
    set obs 1000
    
    local past A C D
    local pastcnt: list sizeof past
    
    // empty local if
    local ifs if
    
    forvalues i = 1 / `pastcnt' {
        gen alpha`i' = char(runiformint(65,68))
        local a : word `i' of `past'
        if `i' < `pastcnt'  {
            local and &
        }
        else local and
        
        // loop to fill up the local ifs
        local ifs `ifs' alpha`i' == `a' `and'
    }    
     
    di "`ifs'"
    The local "ifs" now looks like this:
    Code:
    if alpha1 == A & alpha2 == C & alpha3 == D
    I obviously need A, C and D to have quotes around them for this local "ifs" to work, but the local naturally puts quotes around the whole string within the local, so if I specify instead:

    Code:
    local ifs `ifs' alpha`i' == "`a'" `and'
    I get this:

    Code:
    if alpha1 == A" & alpha2 == "C" & alpha3 == "D"" invalid name
    I've also tried double quotes, e.g. `"`a'"', but that doesn't do the trick either... I am guessing that I am missing something very basic here, but alas, I cannot figure it out!

    Any help is appreciated!

    Ariel

  • #2
    The problem is actually with -display-, not with the definition of -ifs-:

    Code:
    . clear
    
    . set obs 1000
    Number of observations (_N) was 0, now 1,000.
    
    .
    . local past A C D
    
    . local pastcnt: list sizeof past
    
    .
    . // empty local if
    . local ifs if
    
    .
    . forvalues i = 1 / `pastcnt' {
    2. gen alpha`i' = char(runiformint(65,68))
    3. local a : word `i' of `past'
    4. if `i' &lt; `pastcnt' {
    5. local and &amp;
    6. }
    7. else local and
    8.
    . // loop to fill up the local ifs
    . local ifs `ifs' alpha`i' == "`a'" `and'
    9. }
    
    .
    . macro dir
    S_level: 95
    F1: help advice;
    F2: describe;
    F7: save
    F8: use
    S_ADO: BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
    S_StataMP: MP
    S_StataSE: SE
    S_OS: Windows
    S_OSDTL: 64-bit
    S_MACH: PC (64-bit x86-64)
    _ifs: if alpha1 == "A" & alpha2 == "C" & alpha3 == "D"
    _a: D
    _pastcnt: 3
    _past: A C D
    So your code is actually correct. And if you use `ifs' as an -if- qualifier in commands, it will work. Unfortunately, -display- seems to choke on it.

    Added, actually, you can also get -display- to play nicely with it if you change your display command to:
    Code:
    . di `"`ifs'"'
    if alpha1 == "A" & alpha2 == "C" & alpha3 == "D"
    Last edited by Clyde Schechter; 15 May 2025, 18:01.

    Comment


    • #3
      Thank you, Clyde! It seems that I know what I am doing!

      Comment

      Working...
      X