Announcement

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

  • Replace value with var name

    Hello,

    I am trying to make my code a bit more compact.

    I want to create a variable B as an empty string, and I would like it to be replaced with the THE NAME of variable A if the value of variable A is >0. Is there a way to do it without using labels? I am doing this in a loop for hundreds of "A" variables.

    Thank you
    Last edited by Ylenia Curci; 29 Feb 2024, 04:59.

  • #2
    "B" is a single variable, so you will overwrite it several times if you are looping over a number of other variables and doing the replacement. Unless you have several "B" variables. If the latter:

    Code:
    foreach var of varlist ...{
        gen B_`var'= "`var'" if `var'>0
    }
    Otherwise, present a data example that illustrates your problem.

    Comment


    • #3
      Hi Andrew, thank you for your answer. Yes sure, I have several B variable, sorry I did not specified it.

      Comment

      Working...
      X