Statalist


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

st: RE: SV: RE: reading txt-file without end-of-line delimiter and uneven record length


From   "Steichen, Thomas J." <SteichT@RJRT.com>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: SV: RE: reading txt-file without end-of-line delimiter and uneven record length
Date   Thu, 20 Nov 2008 12:57:16 -0500

Yes, I knew the last di was out of the loop (i.e., the "selected" value displayed was only the last value). The intent was to read the data correctly and use di to display a few to know you were reading OK.

Please note that in one place you specified 11 useful characters and 1963 characters of "junk" in the first line (1974 characters total) but elsewhere you claimed a record length of 1975. This first file used the 1974 char specification; a send file sent today uses the 1975 char specification.

Tom

-----------------------------------
Thomas J. Steichen
steicht@rjrt.com
-----------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Even Bergseng
Sent: Thursday, November 20, 2008 12:28 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: SV: RE: reading txt-file without end-of-line delimiter and uneven record length

Thanks Thomas!

That almost did the trick. The last -di- is outside the loop, so it only displays the last macro/variable. Instead of display, I use a -file write- (inside the loop) to a new file where I then can leave out anything from the original file I don't need.

Even



________________________________________
Fra: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] p&#229; vegne av Steichen, Thomas J. [SteichT@RJRT.com]
Sendt: 19. november 2008 23:34
Til: 'statalist@hsphsun2.harvard.edu'
Emne: st: RE: reading txt-file without end-of-line delimiter and uneven record length

Even,

Here is some code that reads the file and displays selected values:

program read_longfile
  version 10.1
  gettoken mname 0 : 0
  syntax using/

* open input file
  tempname hdl
  file open `hdl' using `"`using'"', read binary

* get first record (as strings)
  file read `hdl' %11s val_11 %1963s junk
  di %11.0f real("`val_11'")

* loop over 100 records
  forvalues i = 1(1)100 {

*   get 6-byte var (as string)
    file read `hdl' %6s val_6
    di %3.0f `i' ": " %6.0f real("`val_6'")

*   loop over 151 13-byte vars (as strings)
    forvalues j = 1(1)151 {
      file read `hdl' %13s val_135
    }

  }
  di %13.5f real("`val_135'")

* close input file
  file close `hdl'

  end

* call via: read_longfile, using "filename"



-----------------------------------
Thomas J. Steichen
steicht@rjrt.com
-----------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Even Bergseng
Sent: Wednesday, November 19, 2008 3:00 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: reading txt-file without end-of-line delimiter and uneven record length

Dear listers!

I have a txt-file without end-of-line delimiter and uneven record length that I want to read into Stata. The lack of end-of-lin delimiters and hence all observations on one line, suggests using the _lrecl option of the -infile2- command. The uneven record length suggests otherwise.

There is only one uneven record (1975 bytes) which occurrs at the beginning of the file. All other records are 1969 bytes. There are 100 records excluding the first.

I have tried to use -file read- and then -file write- to get rid of the first uneven record to be able to use -infile-, but the total line length and thus the macro from -file read- makes Stata state "too few quotes" when I try to write it with -file write-.

My code for the -file- command is as follows:

****
tempname OUT1
file open `OUT1' using "$sima\OUT1.dat", read write text
file seek `OUT1' 1975
file read `OUT1' line
file close `OUT1'
tempname RESULT
file open `RESULT' using "$sima\RESULT.dat", read write text
file write `RESULT' (`"`macval(line)'"')
file close `RESULT'
***

Any hints on how th read the txt-file?

best regards,
Even Bergseng

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

CONFIDENTIALITY  NOTE:  This e-mail message, including any attachment(s), contains information that may be confidential, protected by the attorney-client or other legal privileges, and/or proprietary non-public information. If you are not an intended recipient of this message or an authorized assistant to an intended recipient, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution, or reproduction of this message and/or any of its attachments (if any) by unintended recipients is not authorized and may be unlawful.

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

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

CONFIDENTIALITY  NOTE:  This e-mail message, including any attachment(s), contains information that may be confidential, protected by the attorney-client or other legal privileges, and/or proprietary non-public information. If you are not an intended recipient of this message or an authorized assistant to an intended recipient, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution, or reproduction of this message and/or any of its attachments (if any) by unintended recipients is not authorized and may be unlawful.

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



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