Announcement

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

  • Building a list based on variables

    Say for a command, we must specify a minimization period like age15to24(1980(1)1988)
    Code:
    clear *
    
    capture program drop gsheet
    program define gsheet
        syntax anything , key(string) id(string)
        
        local url "https://docs.google.com/spreadsheets/d/`key'/export?gid=`id'&format=csv"
        
        copy "`url'" `anything', replace
        
        noi disp `"saved in `anything'"'
        
        import delim using `anything', clear bindquote(strict)
        
        erase `anything'
    end
    
    gsheet "`c(pwd)'\\test.csv" , key("1NZQSPAD4-TWpqLmhKt3dR8cq82WIh4LqdhXIF33KNK8") id("1600547932")
    cls
    labmask state_fips, values(state)
    
    
    xtset state_fips year, y
    local lbl: value label `r(panelvar)'
    
    loc unit ="California":`lbl'
    
    cls
    
    foreach v of var lnincome retprice age15to24 {
        
    
    local i `v'(1980(1)1988)
    
    di "`i' " _continue
    
    
    }
    di "`i'"
    Where the goal is to build a macro like "lnincome(1980(1)1988) retprice(1980(1)1988) age15to24(1980(1)1988)". When I do di `i' however, we see
    Code:
    . foreach v of var lnincome retprice age15to24 {
      2.         
    . 
    . local i `v'(1980(1)1988)
      3. 
    . di "`i' " _continue
      4. 
    . 
    . }
    lnincome(1980(1)1988) retprice(1980(1)1988) age15to24(1980(1)1988) 
    . di "`i'"
    age15to24(1980(1)1988)
    How might I make a macro that has all these, not just age?

  • #2
    You must append `i' to the existing local i.

    Code:
    clear *
    
    capture program drop gsheet
    program define gsheet
        syntax anything , key(string) id(string)
        
        local url "https://docs.google.com/spreadsheets/d/`key'/export?gid=`id'&format=csv"
        
        copy "`url'" `anything', replace
        
        noi disp `"saved in `anything'"'
        
        import delim using `anything', clear bindquote(strict)
        
        erase `anything'
    end
    
    gsheet "`c(pwd)'\\test.csv" , key("1NZQSPAD4-TWpqLmhKt3dR8cq82WIh4LqdhXIF33KNK8") id("1600547932")
    cls
    labmask state_fips, values(state)
    
    
    xtset state_fips year, y
    local lbl: value label `r(panelvar)'
    
    loc unit ="California":`lbl'
    
    cls
    local i
    foreach v of var lnincome retprice age15to24 {
        
    
    local i `i' `v'(1980(1)1988)
    
    di "`i' " _continue
    
    
    }
    di "`i'"
    Res.:

    Code:
    . di "`i'"
    lnincome(1980(1)1988) retprice(1980(1)1988) age15to24(1980(1)1988)

    Comment

    Working...
    X