Announcement

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

  • Import the text file with stata

    Hello,
    I download U.S bank holding companies quarterly data ( in text file format) from federal reserve bank of Chicago from 2000 to 2016, and after I import it into Stata using:
    "File->import->Textdata(delimited,...) " and for the delimiter I choose custom and "^", Use first row for variable names: "custom", but when I arrive to the 4 quarter of 2007 I receive this message : long invalid varname
    invalid syntax
    Error creating variables

    r(109);



  • #2
    Welcome to Statalist.

    Without seeing your data, we can't be sure what the problem is. Can you open the file in a text editor (like Notepad on Windows or TextEdit on the Mac) and copy the first few lines and then paste that into a post using code delimiters as described below?

    An alternative is to tell Stata to skip the first line(s) with the variable name and start with the first line of data. The variables will be named var1 var2 ... and you will probably want to rename them, but if you're in a hurry it may be the fastest way of getting your data in.

    Code delimiters:

    To present data, code, and results readably, please copy them and paste them into a code block in the Forum editor, as explained in the Statalist FAQ linked to at the top of the page. For example, the following:

    [code]
    // sample code
    sysuse auto, clear
    describe
    [/code]

    will be presented in the post as the following:
    Code:
    // sample code
    sysuse auto, clear
    describe

    Comment


    • #3
      here is few lines of the text file :

      //RSSD9001^RSSD9999^RSSD9007^RSSD9008^RSSD9043^RSSD9 132^RSSD9040^RSSD9032^RSSD9146^BHBC3368^BHBC3402^B HBC3516^BHBC3519^BHBC4070^BHBC4073^BHBC4074^BHBC40 79^BHBC4091^BHBC4093^BHBC4094/

      thank you

      Comment


      • #4
        Please prefer to use code delimiters or - datex - to share data/output/command, as William remarked. As William also underlined, you will find the instructions in the FAQ.

        That said, I wonder whether you tried the ‘automatic’ option, instead of the costumized one.
        Last edited by Marcos Almeida; 25 Dec 2017, 17:00.
        Best regards,

        Marcos

        Comment


        • #5
          thank you Marcos Almeida, please how can I use the automatic option ? ( I have to keep the variables name code, i.e RSSD9146.....)

          there is few lines of text file :
          Code:
          RSSD9001^RSSD9999^RSSD9007^RSSD9008^RSSD9043^RSSD9 132^RSSD9040^RSSD9032^RSSD9146^BHBC3368^BHBC3402^B HBC3516^BHBC3519^BHBC4070^BHBC4073^BHBC4074^BHBC40 79^BHBC4091^BHBC4093^BHBC4094

          Comment


          • #6
            What you showed is not the first few lines of the text file, it is part of the first liine of the file, which was "wrapped" to fit within the window of the program you copied it from. The first line of the file would be the entire list of variable names.

            I was able to find and download the file from the Chicago Fed. The problem is that one of the variable names in the file is "LONG" and the default option of import delimited is to convert all variable names to lowercase, so this becomes "long" and "long" is a reserved word in Stata that cannot be used as a variable name and import delimited is apparently not smart enough make a change and tell you about it.

            You have two choices.

            1) Edit the data file and find LONG and change it to LON, then import the file.
            Code:
            import delimited using bhcf0712.txt, delimiters("^") varnames(1) rowrange(3)
            This corresponds to the File->import->Textdata(delimited,...) menu item you used. Note that I specify the rowrange(3) option so that the second line, full of dashes, is skipped. If I don't use that options, the dashes are read in and cause all the data to be imported as string rather than numeric.

            2) Tell import to leave all the variable names as they are using the case(preserve) option, then use rename to fix things up.
            Code:
            import delimited using bhcf0712.txt, delimiters("^") varnames(1) rowrange(3) case(preserve)
            rename LONG LON
            rename *, lower
            For details on rename, see help rename and help rename group.

            Also, after seeing the file, I understand why you needed to keep the variables names! I didn't imagine there were so many variables.
            Last edited by William Lisowski; 25 Dec 2017, 18:33.

            Comment


            • #7
              thank you very much Wiliam Lisowski

              Comment

              Working...
              X