Statalist


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

Re: st: customised output formats


From   "Svend Juul" <SJ@SOCI.AU.DK>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: customised output formats
Date   Wed, 26 Mar 2008 11:43:20 +0100

Dan wrote:

I have the variables 
Pid, x, y, t, d

For 1000 participants, and there are many observations for each
participant. 

I'm using an archaic program that uses data in the format:

Pid
X,y
X,y
X,y
END
Pid
X,y
X,y
END 

And so on.

I would think that I need to : 

loop through for each pid
  identify the value of pid
  write this value to an outfile

 loop through all observations of current pid
  extract values for x and y
  write x and y values to outfile
 
write "end" into outfile

I tried this logic, but I can't seem to append info to the outfile...
any ideas?

====================================================================

Interesting puzzle. I assume that -t- is some kind of time variable.
I did not try to guess about -d-.

If these are the data:

    . type test1.txt
    127
    12,13
    23,25
    26,30
    END
    128 
    4,45
    7,53
    END

I first read each line as a text string. 
Next I give a number to each observation, starting afresh after 
each "END". The -pid- records get t=0 and the "END" records t=-1.

    infix str15 z 1-15 using test1.txt
    gen t=0 in 1
    replace t=-1 if z=="END"
    replace t=t[_n-1]+1 if _n>1 & z!="END"
    generate str pid=z if t==0
    replace pid=pid[_n-1] if t>0
    generate comma = strpos(z,",") if t>0
    generate x = real(substr(z,1,comma-1)) if t>0
    generate y = real(substr(z,comma+1,.)) if t>0

    . list

         +------------------------------------+
         |     z    t   pid   comma    x    y |
         |------------------------------------|
      1. |   127    0   127       .    .    . |
      2. | 12,13    1   127       3   12   13 |
      3. | 23,25    2   127       3   23   25 |
      4. | 26,30    3   127       3   26   30 |
      5. |   END   -1             .    .    . |
         |------------------------------------|
      6. |   128    0   128       .    .    . |
      7. |  4,45    1   128       2    4   45 |
      8. |  7,53    2   128       2    7   53 |
      9. |   END   -1             .    .    . |
         +------------------------------------+

Finally you can drop the redundant observations and variables:

    keep if t>0
    keep pid t x y

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:  sj@soci.au.dk
__________________________________________ 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index