Announcement

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

  • import csv file to stata

    Dear Statlisters,
    I have a folder that contains 40 csv files (OAEX1 to OAEX40). The first column is the same for all of them, but the second column is different. The columns are not labeled. i have tried the code below and other codes I found online but in vain. What i want to do is to merge all the csv file in one dat file to be able to some statistics etc... Your help is much appreciated.



    Code:
     cd "/Users/aarredouani/Documents/csv"
    clear
    tempfile building
    save `building', emptyok
    
    local myfilelist : dir . files"*.csv"
    foreach file of local myfilelist {
    drop _all
    insheet using `"`file'"'
    local building = subinstr("`file'",".csv","",.)
    merge using "`building'"
    save `building', replace
    }
    Macrpo stat 15.1
    Attached Files
    Last edited by abdelilah arredouani; 15 Jun 2021, 17:34.

  • #2
    I don't follow most of your post, but you can import CSV files using -import delimited-. If you need more specific help, then please post back with a reproducible example of your CSV files and how exactly you want the final product to look like so others may better help you.

    Comment


    • #3
      I think the problem is a confusion of "merge" and "append". Did you want to append the 47 files into one file with many observations, or join them into one file with many variables? If the later, your merge statement needs to have variable to merge by. that is present in all the files. If the former, use -append- rather than -merge-. Note that you can append to an empty workspace (thank you Stata). I expect this confusion started because SAS and SQL picked different meanings for the same commands way back in the 1970s and Stata followed SAS.

      Comment

      Working...
      X