Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: importing "irregular" columns with embedded blanks


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   RE: st: importing "irregular" columns with embedded blanks
Date   Wed, 24 Jan 2007 10:48:51 +0100

Buzz wrote:

I have data that comes to me in a text file that is generally columnar,
but
does not contain any delimiters.  If I import the file into Stata it
comes
in as a single variable v1 ; the first 10 observations look something
like
this (assuming the email doesn't mangle it here):
 
. list v1

 
+----------------------------------------------------------------------
     |
v1 
 
|----------------------------------------------------------------------
  1. |  ANY 1206 Bunk1 Corn Silage      $0.00     97    FarmGrown Forag
lb
  2. |      1234 Shed1 Grass Hay        $0.00    146    Purchased Forag
lb
  3. |      1582 Purch1 Straw           $0.00    164    Purchased Forag
lb
  4. |      1237 Shor1 Haylage 1st cut  $0.00    149    FarmGrown Forag
lb
  5. |      1238 Bunk4 Haylage 2nd cut  $0.00    150    FarmGrown Forag
lb
 
|----------------------------------------------------------------------
  6. |      1070 CornGrainGrndFine    $135.00    205    Purchased Energ
lb
  7. |       039 BakeryByProdBread    $130.00    308    Purchased Energ
lb
  8. |      1052 BeetPulpPlCp         $140.00    313    Purchased Energ
lb
  9. |      1022 EnergyBooster        $1,200.    521    Purchased Energ
lb
 10. |
00

--------------------------------------------------------------------

This looks like fixed format data; use -infix- to read it. I first
created a text file (buzz.txt) from your list:

 ANY 1206 Bunk1 Corn Silage      $0.00     97    FarmGrown Forag   lb
     1234 Shed1 Grass Hay        $0.00    146    Purchased Forag   lb
     1582 Purch1 Straw           $0.00    164    Purchased Forag   lb
     1237 Shor1 Haylage 1st cut  $0.00    149    FarmGrown Forag   lb
     1238 Bunk4 Haylage 2nd cut  $0.00    150    FarmGrown Forag   lb
     1070 CornGrainGrndFine    $135.00    205    Purchased Energ   lb
      039 BakeryByProdBread    $130.00    308    Purchased Energ   lb
     1052 BeetPulpPlCp         $140.00    313    Purchased Energ   lb
     1022 EnergyBooster        $1,200.    521    Purchased Energ   lb
                                                                   00

The command goes like this:

clear
infix str v1 1-4 num1 5-9 str name1 11-31 str sprice 32-38 ///
   num2 39-45 str name2 50-64 str name3 68-69 using buzz.txt

// remove $ and , from price and make it numeric
replace sprice = subinstr(sprice,"$","",.)
replace sprice = subinstr(sprice,",","",.)
generate price = real(sprice)
format price %8.2f


Hope this helps
Svend
__________________________________________

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone: +45 8942 6090
Home:  +45 8693 7796
Email: [email protected]
__________________________________________ 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index