Announcement

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

  • Each individual in separate .csv files, how to import and name them all?

    Right now I have data for multiple individuals spanning 365 days. Currently the files are saved like: (I made up the numbers here, it's not exactly height and weight I'm using)
    person1.csv - inside is just delimited like
    date;height;weight
    1994-01-01;165;79.0
    1994-01-02;165;79.3
    ... and so on.

    Then the second file person 2.csv, with date and variables separated with ;. And so on to the 200th person.

    What I'm having trouble is trying to name them all when imported to Stata, for example I want Stata to recognize them as
    person 1;1994-01-01;165;79.0
    person1;1994-01-02;165;79.3
    ...
    person2;1994-01-01;140;60.0
    person2;1994-01-02;141;60.3
    ...
    person200;1994-01-01;150;70.0
    ...
    person200;1994-12-31;151;74.0

    Something to that effect, so I can run fixed effects or other panel estimations. I was wondering how this is possible? Thank you in advance!

  • #2
    So let's say, to illustrate, you have 200 people in 200 files named person1.csv through person200.csv.

    Code:
    clear*
    tempfile building
    save `building', emptyok
    forvalues i = 1/200 {
        import delimited person`i'.csv, delimiters(";") varnames(1) clear // POSSIBLY OTHER OPTIONS
        gen person = "person`i'"
        append using `building'
        save `"`building'"', replace
    }
    save combined_person_file, replace
    Note: Not tested, beware of typos.

    As an aside, the resulting file will contain all of the data from the individual files, including any data errors they contain, and any problems with clashes of variable names, storage types, labels, etc. Such a file can be a nightmare to clean. So I advise you to carefully clean the individual data sets before you combine them, and in the process make them all conform to the same scheme of variable names, storage types, value labels, formats, etc.



    Comment


    • #3
      [EDIT] I just got the date to work! I was ordering some things wrong. Thanks again for the help!

      Hello, thank you! it worked (with some modifying of names). I was also wondering, how could I turn dates in 1990-01-02 format to a date variable? I have tried destring, date, with varying success.
      Last edited by Shu Chang; 30 Nov 2017, 21:35.

      Comment


      • #4
        I'm amazed that you have had any success with -destring-ing something like 1990-01-02. I would think you get only missing values.

        Stata date and time variables are complicated, and if you are going to be a regular Stata user, you will have to bite the bullet and learn about them. The documentation in the PDF manuals that come with your Stata installation is extensive. You need to read that entire section of the manuals. It's a long read and there's a lot there. You won't remember it all, but you will at least become familiar enough with how Stata approaches this that you will probably recognize what particular functions are likely to be what you need in most circumstances; then you can refer back to the documentation for details.

        In this case, what you need is:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str10 string_date_var
        "1990-01-02"
        end
        
        gen real_date_variable = date(string_date_var, "YMD") // OR IS IT "YDM"?
        format real_date_variable %td
        I cannot tell whether your example, 1990-01-02 is supposed to be 2 January or February 1. The code above assumes the former. If it's supposed to be the latter, replace "YMD" by "YDM".

        Added: This question has no real connection to the original topic. In the future, when changing subjects, please start a new thread. If somebody wants to search for information about date variables, they won't find it here. We need to keep the threads on topic so that others can find them when searching.

        Comment


        • #5
          Cross-posted at https://www.reddit.com/r/stata/comme..._files_how_to/

          The FAQ Advice makes explicit our policy on cross-posting, which is that you should tell us about it.

          Comment


          • #6
            @Clyde Schechter Thank you for the additional comments, and I will be sure to keep it in mind to post in a new thread in the future.

            @Nick Cox
            My bad, I completely missed that part in the FAQ. I have read it now and I will not do this again in the future (without linking so that people may access other good answers easily).

            Thank you for the patience and understanding and I will do my best to make this forum a better place as well. Cheers!

            Comment

            Working...
            X