Announcement

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

  • import many files?

    Dear All,
    1. I have a lot of files (such as t201304016.ftm, t201304026.ftm, and so on) in the same directory to be imported into Stata. The file `t201304016.ftm' contains intra-daily data on April 1 (0401), 2013, and `t201304026.ftm' on April 2 (0402), 2013. The last `6' in each file name does not have any meaning, though.
    2. I tried the following (I do not post the data set because it is big, and is not in Stata format):
      Code:
      fs *.ftm
      	foreach v in `r(files)' {
      	infix x1       1-6     ///
      	        x2       7-14    ///
      	        str code 15-20   ///
      	        th       21-22   ///
      	        tm       23-24   ///        
      	        ts       25-26   ///
      	        tt       27-33   ///
      	        p        34-39   ///
      	        v        40-47   ///
      	        b1       48-53   ///
      	        b1v      54-61   ///
      	        b2       62-67   ///
      	        b2v      68-75   ///
      	        b3       76-81   ///
      	        b3v      82-89   ///
      	        b4       90-95   ///
      	        b4v      96-103  ///
      	        b5       104-109 ///
      	        b5v      110-117 ///
      	        s1       118-123 ///
      	        s1v      124-131 ///
      	        s2       132-137 ///
      	        s2v      138-145 ///
      	        s3       146-151 ///
      	        s3v      152-159 ///
      	        s4       160-165 ///
      	        s4v      166-173 ///
      	        s5       174-179 ///
      	        s5v      180-187 ///
      	  using `v', clear
      	  keep if code == "0050"
      	  gen d = `v'  
      	  egen t = group(x2)  
      	  order d t, first
      	  save `v'.dta, replace
      	  clear
      	}
      but got error message
      Code:
      . fs *.ftm
      	t201304016.ftm  t201304026.ftm
      	
      	. foreach v in `r(files)' {
      	  2. infix x1       1-6     ///
      	>         x2       7-14    ///
      	>         str code 15-20   ///
      	>         th       21-22   ///
      	>         tm       23-24   ///        
      	>         ts       25-26   ///
      	>         tt       27-33   ///
      	>         p        34-39   ///
      	>         v        40-47   ///
      	>         b1       48-53   ///
      	>         b1v      54-61   ///
      	>         b2       62-67   ///
      	>         b2v      68-75   ///
      	>         b3       76-81   ///
      	>         b3v      82-89   ///
      	>         b4       90-95   ///
      	>         b4v      96-103  ///
      	>         b5       104-109 ///
      	>         b5v      110-117 ///
      	>         s1       118-123 ///
      	>         s1v      124-131 ///
      	>         s2       132-137 ///
      	>         s2v      138-145 ///
      	>         s3       146-151 ///
      	>         s3v      152-159 ///
      	>         s4       160-165 ///
      	>         s4v      166-173 ///
      	>         s5       174-179 ///
      	>         s5v      180-187 ///
      	>   using `v', clear
      	  3.   keep if code == "0050"
      	  4.   gen d = `v'  
      	  5.   egen t = group(x2)  
      	  6.   order d t, first
      	  7.   save `v'.dta, replace
      	  8.   clear
      	  9. }
      	(3,142,057 observations read)
      	(3,141,307 observations deleted)
      	t201304016:  operator invalid
      	r(198);
      	
      	end of do-file
      	
      	r(198);
    3. In fact, I want to extract only one stock (code=0050), and save all the files for each day with name `v' (later to be appended altogether). Any suggestions is highly appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Your generate command is failing because you omitted quotation marks around the non-numeric value being assigned. The particular error message is because the embedded period caused Stata to attempt to parse it as factor variable or time series notation.
    Code:
    . clear
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . local v t201304016.ftm
    
    . generate d = `v'
    t201304016:  operator invalid
    r(198);
    
    . generate d = "`v'"
    
    . list, clean
    
                        d  
      1.   t201304016.ftm

    Comment


    • #3
      Dear William, Thank you for the reply. I will give it a try ASAP.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Dear William, It works. Thanks a gain. However, as you can find below, two files are saved as `t201304016.ftm.dta' and `t201304026.ftm.dta'. Is it possible to save them as `t201304016.dta' and `t201304026.dta'? (remove the ftm.)
        Code:
        . fs *.ftm
        t201304016.ftm  t201304026.ftm
        
        . foreach f in `r(files)' {
          2.   infix x1       1-6     ///
        >         x2       7-14    ///
        >         str code 15-20   ///
        >         th       21-22   ///
        >         tm       23-24   ///        
        >         ts       25-26   ///
        >         tt       27-33   ///
        >         p        34-39   ///
        >         v        40-47   ///
        >         b1       48-53   ///
        >         b1v      54-61   ///
        >         b2       62-67   ///
        >         b2v      68-75   ///
        >         b3       76-81   ///
        >         b3v      82-89   ///
        >         b4       90-95   ///
        >         b4v      96-103  ///
        >         b5       104-109 ///
        >         b5v      110-117 ///
        >         s1       118-123 ///
        >         s1v      124-131 ///
        >         s2       132-137 ///
        >         s2v      138-145 ///
        >         s3       146-151 ///
        >         s3v      152-159 ///
        >         s4       160-165 ///
        >         s4v      166-173 ///
        >         s5       174-179 ///
        >         s5v      180-187 ///
        >   using `f', clear
          3.   keep if code == "0050"
          4.   gen d = "`f'"  
          5.   egen t = group(x2)  
          6.   order d t, first
          7.   save `f'.dta, replace  
          8. }
        (3,142,057 observations read)
        (3,141,307 observations deleted)
        (note: file t201304016.ftm.dta not found)
        file t201304016.ftm.dta saved
        (2,969,879 observations read)
        (2,969,102 observations deleted)
        (note: file t201304026.ftm.dta not found)
        file t201304026.ftm.dta saved
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Replace your -save `f'.dta, replace- command with the following two lines:
          Code:
          local savename: subinstr local f ".ftm" ""
          save `savename'.dta, replace

          Comment


          • #6
            Dear Clyde, Thanks a lot. It works quite well.

            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment

            Working...
            X