Announcement

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

  • Creating binary variable with multiply conditions using for loop

    I want to create a binary variable using for loop. The setup is that my binary variable, say "a", will take the value 1 if any one of the variables b,c or d take the value 5. I tried doing it using a local macro for b,c,d. But I found that stata is only reading variable b and not the other ones. I know this can be easily done using a replace and generate commands, but I am new to writing loops and hence want to learn this approach.

  • #2
    You don't show any code which makes this hard to follow. I can't see that loops or local macros or replace need enter here at all. In principle

    Code:
    gen byte a = inlist(5, b, c, d)
    is a solution without any loop or locals. https://www.stata-journal.com/articl...article=dm0058 says more. byte isn't essential here but is good practice.

    If you want an explanation of why your code didn't work as you wanted or expected, then you need to show it to us. You may also need to include a data example.

    Comment

    Working...
    X