----- Original Message -----
From: Barbara Hofmann <[email protected]>
Date: Wednesday, January 12, 2005 5:14 am
Subject: st: How to read in an irregular ASCII-file?
> Hello,
>
> hopefully someone of you can help me with the following:
>
> i want to read in a ascii-dataset of a structure like this:
>
> 104525300010 9064496 022 999929912
> 204525300010 9064496 1:19 59 22 2:4 0 96 3:8 0 92 4:0 33 67
> 204525300010 9064496 6:30 50 20 7:78 5 17
> 104525300010 9081400 023 23131122
> 104525300020 7 332321332 06 00 05 2264
> 104525300020 8 221211121 06 00 09 1889
> 104525300020 8 222222222 06 00 08 4705
> 104525300020 8 231211222 06 00 06 4585
>
>
> "2" in the first column means that this line belongs to the line
> before. column 31-39 include replies to 9 different questions. if
> it is a "9" this means, that in the following line there is a more
> specific answer to this question which is stored like this:
> 7:78 5 17 means "78% replied to question 7 with a "1", 5% replied
> to question 7 with a "2" and 17% replied to question 7 with a "3""
>
> Then ":" signalizes that the answers to the question of the number
> before ":" will follow. How can i tell Stata this meaning of ":"?
> Maybe also a problem could be that the answers can be like "10 90
> 0" as well as like "15 15 70" or "5 5 90"?
>
> Does anybody have an idea how to read in those dates?
>
> Many thanks!
>
> Barbara Hofmann
What do you want the data to look like in the end?
As a start, the commands below, I believe, pull out the relevant information.
It uses Michael Blasnik and Nick Cox’s –strparse- command.
insheet using "C:\Documents and Settings\scott.merryman\Desktop\foo.txt", clear
gen id = substr(v1, 1,1)
forv i = 31/39 {
gen question`i' = substr(v1, `i', 1) if id == "1"
}
gen answer = substr(v1, 23,.) if id == "2"
strparse answer, g(b)
forv i = 1/12 {
strparse b`i' , gen(detail`i') parse(:)
}
drop b*
Hope this helps,
Scott
*
* 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/