Create variables in a loop

It’s better to use dictionaries in this case

dict = Dict{Symbol, Any}()
vars = Dict{String, Symbol}("GDP" => :y, 
                                            "PCEC" => :c,
                                            "GPDI" => :i,
                                            "GCE" => :g, 
                                            "NETEXP" => :nx)
for (k, v) in vars
   d = get_data(f, k)
   dict[v] = d.df[:4]
end

# example of using variable
dict[:nx] # some value

# Or even
dict[vars["NETEXP"]] # the same as dict[:nx]

If you know types in advance it’s better to use them instead of Any.

4 Likes