Announcement

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

  • Assign values for a macro based on an if condition that contains a variable (StataSE 17)

    Hello,

    I would like to assign values for a macro based on an if condition that contains a normal string variable. Then the for loop will loop through and replace the macro depending on the name the loop is on.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str20 var float r2_m1
    "ages"    .28059122
    "bmi"      .3975436
    "dbp"      .4121804
    "hdl"      .3830052
    "lncreat"  .3973525
    "lncrp"    .4424658
    "lntrig"   .4124405
    "sbp"      .3837971
    "tchol"    .3834501
    end

    local cov2 = "lntrig bmi"
    foreach cov of local cov2 {
    if "`cov'"==var local r2 = r2_m1
    di "`cov'"
    di "`r2'" _newline
    }

    I would like the above to display

    lntrig
    .4124405

    bmi
    .3975436

    However, it displays the following instead. 0.28059... seems to be the value for ages.

    lntrig
    .28059121966362

    bmi
    .28059121966362


  • #2
    Not sure why you will want to do this, but..

    Code:
    Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20 var float r2_m1
    "ages"    .28059122
    "bmi"      .3975436
    "dbp"      .4121804
    "hdl"      .3830052
    "lncreat"  .3973525
    "lncrp"    .4424658
    "lntrig"   .4124405
    "sbp"      .3837971
    "tchol"    .3834501
    end
    
    forval i=1/`=_N'{
        local `=var[`i']' `=r2_m1[`i']'
    }
    
    di "`lntrig'"
    di "`bmi'"
    Res.:

    Code:
    . di "`lntrig'"
    .4124405086040497
    
    . 
    . di "`bmi'"
    .3975436091423035

    Comment


    • #3
      In an if command, as opposed to an if qualifier, a reference to a variable name is in many cases interpreted as a reference to its value in the first observation. Thus first time round the loop,

      if "`cov'"==var local r2 = r2_m1

      is interpreted as

      Code:
      if "intrig"==var[1]  local r2 = r2_m1
      Here the if expression is false, so the local won't get defined, and the results should make sense knowing this.

      Also, the loop in #1 is a loop over two variable names. There is no sense in which it is also a loop over all the observations of a dataset. As Andrew Musau points out, if you want that you need to spell out your wish with explicit syntax. I have never used SAS, but I have a lingering impression from not using it that SAS supports that quite different kind of loop.

      This often confuses. There is an old FAQ at https://www.stata.com/support/faqs/p...-if-qualifier/ which in my view is backwards as the question is really the answer.

      The redoubtable Clyde Schechter and I have a lengthier piece on this forthcoming in Stata Journal 23(2), which I guess will hit the newstands in late June or early July, which is no use to you now, but is possibly a useful detail if people look at this thread afterwards.

      Comment

      Working...
      X