Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65068

How to locally store the particular position of an observation for its further use in a loop

$
0
0

Dear all, I´d like to compare the Values in two columns and if they mach, i´d like to know the difference between their timestamp.

I have a dataset that looks like this:
clear

input str42 (pos ppos timeSIF)

"15" "0" "1648933615933"
"16" "0" "1648933615937"
"18" "0" "1648933615940"
"17" "0" "1648933615940"
"91" "15" "1648933618373"
"92" "15" "1648933618390"
"93" "16" "1648933618427"
"94" "0" "1648933616773"
"95" "0" "1648933616773"
"96" "18" "1648933618373"
"97" "18" "1648933618390"
"98" "19" "1648933618427"

end

In a first step, I compared each value in pos against all the values of ppos in a loop.
Therefore I´ve created the variable s which already signals when a match is found.

Code:
gen s=0

.
.  quietly forval i = 1/`=_N'  {
.
.         replace s=1 if pos[`i' ] == ppos
    
. }
list.

+--------------------------------+
| pos ppos timeSIF s |
|--------------------------------|
1. | 15 0 1648933615933 0 |
2. | 16 0 1648933615937 0 |
3. | 18 0 1648933615940 0 |
4. | 17 0 1648933615940 0 |
5. | 91 15 1648933618373 1 |
|--------------------------------|
6. | 92 15 1648933618390 1 |
7. | 93 16 1648933618427 1 |
8. | 94 0 1648933616773 0 |
9. | 95 0 1648933616773 0 |
10. | 96 18 1648933618373 1 |
|--------------------------------|
11. | 97 18 1648933618390 1 |
12. | 98 19 1648933618427 0 |
+--------------------------------+

Furthermore i´d like to replace the value s=1 for the difference between the timestamp of pos and ppos.

In other words, I want to do something like this
(this is of course pseudocode; I understand that i´t would not work
like this):
Code:
  
 gen s=0  
  quietly forval i = 1/`=_N'  {  
         replace s= timeSIF [`i' ]- timeSIF [$$$] if positionid[`i' ] == parentpositionid [$$$]  
         }

I would like to know how to locally store the particular position ([$$$]) of the observation ppos that matches pos so that in a third I get the difference of the timestamp
As an example I would like to have following output for s for the first and second match -2,440
-2,457
Any suggestions?

Viewing all articles
Browse latest Browse all 65068

Trending Articles