| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Keeping significant spaces with -infile- and -infix-
Joseph Coveney wrote:
How can significant unimbedded spaces of string variables be retained when
importing fixed-width ASCII files?
Last year I brought this up with Stata Tech Support and got the reply below from Kevin Crow. The application I was working on needed to be automated in a way that precluded using Kevin's approach. I ended up going through SPSS first, then converting to Stata, since SPSS preserves leading and trailing blanks. What I'd like to see in Stata is something like a "notrim" option on -infile- that would keep leading and trailing blanks.
----------------------------
Dear Phil,
You might try using the -file- command to preserving leading blanks when
importing your text data into Stata. -file- is a programmer's command and
should not be confused with outfile, outsheet, -insheet-, -infile-, or -infix-,
which are the usual ways data is imported and exported in Stata. -file- allows
programmers to read and write both ASCII text and binary files, so -file- could
be used to write a program to input data or export data in some complicated
situation. For further assistance using the -file- command type -help file- in
your Command window.
----begin log file----
. clear
. tempname fh
. file open `fh' using vcal.dat, read
. file read `fh' line
. local j = 1
. set obs 1
obs was 0, now 1
. while r(eof)==0 {
2.
. if `j' == 1 {
3. generate casid = substr("`line'", 1, 15) in `j'
4. generate vcol_1 = substr("`line'", 18, 1) in `j'
5. generate vcal_1 = substr("`line'", 19, 79) in `j'
6. file read `fh' line
7. generate vcol_2 = substr("`line'", 18, 1) in `j'
8. generate vcal_2 = substr("`line'", 19, 79) in `j'
9. }
10.
. file read `fh' line
11. file read `fh' line
12. file read `fh' line
13. file read `fh' line
14.
. }
. list
+------------------------------------------------------------------------+
1. | casid | vcol_1 |
| 2 6 3 | 1 |
|------------------------------------------------------------------------|
| vcal_1 |
| 1111111BPPPPPPPP0000000000000000000111111111111111111111111.. |
|------------------------------------------------------------------------|
| vcol_2 | vcal_2 |
| 2 | 2 |
+------------------------------------------------------------------------+
. gen stl=strlen(vcal_2)
. list stl vcal_2
+-----------------------------------------------------+
| stl vcal_2 |
|-----------------------------------------------------|
1. | 45 2 |
+-----------------------------------------------------+
----end log file----
Sincerely,
Kevin Crow
--
Phil Bardsley <[email protected]>
Research Programming Services
Carolina Population Center
University of North Carolina
Chapel Hill, NC 27516 USA
919-966-2825
*
* 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/