On Fri, 25 Apr 2003, Fred Wolfe wrote:
> At 12:06 PM 4/25/2003 -0700, you wrote:
> >Hello,
> >
> >I am very new to Stata and am having some difficulty wrapping my brain
> >around Stata's methods of data processing. I would like to be able to
> >process a dataset line by line rather then all at once. I have found many
> >references to the fact that "if" statements don't work as expected and a
> >very good comparison between STATA and SAS which briefly mentions some of
> >the paradigm shifts.
> >
> >My question to the group is how would you deal with the line by line
> >processing issue? I need to change each line based on a calulated value
> >that is dependant upon data in that line.
> >
> >The "if" qualifier would seem to solve this for some cases, but not when a
> >program has to be called or a really complex comparison has to be done.
>
>
> Nick's reply is to indicate that you really don't need to loop over
> observations. People coming from SAS very often think that you do. Using
> Stata, in some instances you might need to create an intermediate variable.
> Following up on Nick's reply, we have a problem thinking of cases where you
> really need to do it the way you suggest. Perhaps a specific example would
> clarify your problem for us.
>
> Fred Wolfe
Thanks Fred and Nick,
Here's the actual example. Given a data set that looks like
A1 , A2 , A3 , A4 , A5 , a6
row1 1 3 . 6 3 4
row2 3 4 2 . 1 5
row3 4 5 2 5 6 2
I need to pull out the top 3 values and place them into B1, B2 and B3
so that
B1 , B2 , B3
row1 6 4 3
row2 5 4 3
row3 6 5 5
I don't actually come from a SAS background, I've used SPSS and SYSTAT and mostly program in Perl.
The way I would normally[1] solve this is to create several place holder variables, (please forgive the psudocode)
z1 = z2 = z3 = 0
and counters
i = j = 1
while i <= 3 {
while j <= 6 {
checkfoo `j' `z1' `z2' /* returns r(checked)=1 if z1 or z2 == j*/
replace B`i' = A`j' if A`j' > B`i' & r(checked) == 0
replace z`i' = `j' if A`j' > B`i' & r(checked) == 0
j = `j' + 1
}
i = `i' + 1
}
when i = 1 the location of the greatest value is stored in z1 and the value in B1
when i = 2 the location of the greatest value is stored in z2 if the location is not the same as the one stored in z1, the value is stored in B2
when i = 3 the value of the greatest value is stored in B3 if the location is not the same as the ones stored in z1 or z2
Thanks for all the help!
Daniel "R" Sabath
[not Daniel "E" Sabath :)]
[1] ok. ok. The way i would actually solve this is
while (<INFILE>) {
@var = $A1,$A2,$A3,$A4,$A5,$A6;
sort @var;
$i = 0;
while ($i < 3) {
$B[$i] = $var[$i];
$i++;
}
}
*
* 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/