Announcement

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

  • Including quotes in a local macro statement

    Hello,

    I would like the following to drop a certain cohort for only one of the files but not the other. It seems that when I ask it to display "`drop_study'" it only shows


    if cohort == study9
    and not
    if cohort == "study9"

    local filepaths = "filepath1 filepath2"

    foreach filepath of local filepaths{
    use "`filepath'", clear
    if regexm("`filepath'","1") local drop_study = `"if cohort == ""study9"""'
    if regexm("`filepath'","2") local drop_study = `""'

    drop `drop_study'
    }

  • #2
    You are being deceived by the display command, which strives to evaluate what it is displaying. The macro list command is the effective way to see exactly what will be substituted when the macro is expanded.
    Code:
    . * as given in example: ""study9""
    
    . local drop_study = `"if cohort == ""study9"""'
    
    . display `" `drop_study' "'
     if cohort == ""study9"" 
    
    . display  " `drop_study' "
     if cohort == study9 
    
    . macro list _drop_study
    _drop_study:    if cohort == ""study9""
    
    . * one less pair of quotes: "study9"
    
    . local drop_study = `"if cohort == "study9""'
    
    . display `" `drop_study' "'
     if cohort == "study9" 
    
    . display  " `drop_study' "
     if cohort == study9" " invalid name
    r(198);
    
    . macro list _drop_study
    _drop_study:    if cohort == "study9"
    
    .

    Comment


    • #3
      Thank you for this William!

      Comment

      Working...
      X