Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a loop to sort data together
    primarykey
    data
    text
    <p>I have the following data:</p> <pre><code> id date day_1...day_6...day_20 port_ent port_day_1...port_day_6...port_day_20 1 1/1/00 2 4 6 1 2 4 6 2 1/6/00 1 2 5 6 . 2 5 3 1/16/00 3 2 1 16 . . 1 4 1/20/00 6 3 2 20 . . 2 </code></pre> <p>I am trying to create the variables <code>port_day_i</code>.</p> <p><code>day_i</code>: given the <code>date</code> how many widgets were sold in the i <em>th</em> day (when <code>i</code> = 1 this is the same day as <code>date</code>)</p> <p><code>port_ent</code>: a standardized date system that uses <code>id = 1</code> as <code>port_day_1</code></p> <p><code>port_day_i</code>: on that portfolio day what were the sales from each individual</p> <p>The day you enter the portfolio the data from your <code>day_1</code> is used and then the next day your <code>day_2</code> etc.</p> <p>I thought this loop would work but it doesn't:</p> <pre><code>forval j = 2/100 { local i = `j' - port_ent + 1 gen port_day_`j' = . replace port_day_`j' = day_`i' if `i' &gt; 0 } </code></pre> <p>This code would get me part way there but obviously this would be very cumbersome and I have over 1000 days of data</p> <pre><code>gen port_day_1 = . replace port_day_1 = day_1 if port_ent == 1 gen port_day_2 = . replace port_day_2 = day_2 if port_ent == 1 replace port_day_2 = day_1 if port_ent == 2 gen port_day_3 = . replace port_day_3 = day_3 if port_ent == 1 replace port_day_3 = day_2 if port_ent == 2 replace port_day_3 = day_1 if port_ent == 3 gen port_day_4 = . replace port_day_4 = day_4 if port_ent == 1 replace port_day_4 = day_3 if port_ent == 2 replace port_day_4 = day_2 if port_ent == 3 replace port_day_4 = day_1 if port_ent == 4 gen port_day_5 = . replace port_day_5 = day_5 if port_ent == 1 replace port_day_5 = day_4 if port_ent == 2 replace port_day_5 = day_3 if port_ent == 3 replace port_day_5 = day_2 if port_ent == 4 replace port_day_5 = day_1 if port_ent == 5 gen port_day_6 = . replace port_day_6 = day_6 if port_ent == 1 replace port_day_6 = day_5 if port_ent == 2 replace port_day_6 = day_4 if port_ent == 3 replace port_day_6 = day_3 if port_ent == 4 replace port_day_6 = day_2 if port_ent == 5 replace port_day_6 = day_1 if port_ent == 6 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload