Announcement

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

  • How to create a for loop for a variable, then append the label name to a stored value

    For example: I have variable country with values 1 = 'sweden', 2 = 'austria', 3 = 'france'
    I want to run a for leap for a regression per country and afterwards store a scalar (coefficients) but would want to append the name of the countries to the scalarname
    what I'm doing now is

    foreach num in numlist 1/3 {
    reg y x1 x2 if country=`num'
    scalar 'num'beta = _b[x1]
    }

    It saves the scalars as 1beta etc. but I would love for it to save it as swedenbeta.

  • #2
    Code:
    decode country, gen(cname)
    replace cname= strtoname(cname)
    forval c= 1/3{
        reg y x1 x2 if country==`c'
        levelsof cname if country==`c', local(name) clean
        scalar `name'beta=_b[x1]
    }
    Last edited by Andrew Musau; 10 Dec 2021, 14:53.

    Comment

    Working...
    X