Announcement

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

  • Create id variable

    Dear Stata experts,

    I'm new to stata and need some help for my analysis.
    This is my problem:
    I've a dataset made of by 17 variables (Year, Winter, spring, summer, autumn, january, february, march, april, may, ..... december).
    All these variables refer to a specific weather station. There are 8 weather stations (8 .dta files).
    For each weather station there are the same 17 variables, but with different values.
    I want to create a id variable for each dta file with name of weather station.

    I hope I was clear.
    I'm grateful for all the help you can give.

  • #2
    for each file you need something like:
    Code:
    gen id = "Boston"
    replace Boston with the name of the weather station

    I assume you will then append the 8 files into one, correct?

    note that further advice is probably possible if one knows more about your data and where you are going; the FAQ has advice on asking questions and you should read it

    Comment


    • #3
      Thank Rich, It's correct.
      Sorry i'll read the FAQ

      Comment


      • #4
        An automated way of creating the id variable and appending all the fiels, which is what you seem to want, is:
        Code:
        **change directory to the directory holding your dta files
        cd "C\somepath\"
        clear
        **create list of all dta files in that directory**
        local myfilelist : dir . files "*.dta"
        
        **For each of the files, generate an id variable
        foreach file of local myfilelist {
        clear
        use `file'
        local fileid = subinstr("`file'",".dta","",8) 
        gen id="`fileid'" 
        save `file', replace 
        }
        clear
        **append the files into one file.
        foreach file of local myfilelist{
        append using `file'
        save appended, replace
        }

        Comment

        Working...
        X