Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Importing subset of a pipe delimited textfile


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   Re: st: Importing subset of a pipe delimited textfile
Date   Wed, 17 Oct 2012 22:15:33 +0900

Rob Shaw wrote:

Thanks. Yes that would work but the problem is the varying length of
each line. So I need to get filefilter or another command to do one
of:

[snip]

Is there a way to use -filefilter- to achieve this?

File sample:

1|ABCD|23|XYZ
10|BCED|1|YZX
30|DCHS|234|YBH

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

If your file sample is representative, that is, if the string variables are of
fixed length as shown, then something like that below would work.  (Start at 
"Begin here".)

Joseph Coveney

. input str15 a 

                   a
  1. "1|ABCD|23|XYZ"
  2. "10|BCED|1|YZX"
  3. "30|DCHS|234|YBH"
  4. end

. quietly compress

. 
. tempfile tmpfil0

. outfile using `tmpfil0', noquote wide

. 
. type `tmpfil0'
1|ABCD|23|XYZ    
10|BCED|1|YZX    
30|DCHS|234|YBH  

. 
. *
. * Begin here
. *
. tempfile tmpfil1

. filefilter `tmpfil0' `tmpfil1', from("|") to(\032d\Q\032d)

. 
. tempfile tmpfil2

. filefilter `tmpfil1' `tmpfil2', from(\r\n) to(\Q\r\n)

. 
. type `tmpfil2'
1 " ABCD " 23 " XYZ    "
10 " BCED " 1 " YZX    "
30 " DCHS " 234 " YBH  "

. 
. infile a str6 b c str20 d using `tmpfil2', clear
(3 observations read)

. quietly replace b = trim(b)

. quietly replace d = trim(d)

. quietly compress

. 
. list, noobs

  +-----------------------+
  |  a      b     c     d |
  |-----------------------|
  |  1   ABCD    23   XYZ |
  | 10   BCED     1   YZX |
  | 30   DCHS   234   YBH |
  +-----------------------+

. 
. exit

end of do-file


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index