-bysort co (month):gen x = sale - (sale[1] + sale[2])/2 if mon>2-
For example:
. input co month sale
co month sale
1. 1 1 100
2. 1 2 50
3. 1 3 200
4. 1 4 15
5. 2 1 15
6. 2 2 55
7. 2 3 0
8. 2 4 0
9. end
. bysort co (month):gen x = sale - (sale[1] + sale[2])/2 if mon>2
(4 missing values generated)
. l, sepby(co) noobs
+-------------------------+
| co month sale x |
|-------------------------|
| 1 1 100 . |
| 1 2 50 . |
| 1 3 200 125 |
| 1 4 15 -60 |
|-------------------------|
| 2 1 15 . |
| 2 2 55 . |
| 2 3 0 -35 |
| 2 4 0 -35 |
+-------------------------+
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Rajesh Tharyan
> Sent: Tuesday, February 07, 2006 8:12 PM
> To: [email protected]
> Subject: st: create a vector from observations
>
>
> Hi,
>
>
> I have a data set of companies and sales for each of the 12 months of the
> year
>
> The dataset looks like this
>
> Co�� �month �������sales
> 1��������� 1��������� 100
> 1��������� 2��������� 50
> 1��������� 3��������� 200
> 1��������� 4��������� 15
>
>
> I take the mean of the sales in the first two months (only for the first
> two
> months) and generate a variable X
> Which is the difference between this mean sales and the actual sales for
> each month after the second month.
>
> The final data set should look like this.
>
> Co�� �month ������sales�� meansales����� X
> 1��������� 1����� 100���������� 75������� .
> 1��������� 2������50������ ���� 75������� .
> 1��������� 3����� 200����� ���� 75�������125
> 1��������� 4������15������� ��� 75��������-60
>
>
> 1. Is there a simple way to do this???
>
> 2. Instead of creating a variable meansales can I store the value as a
> scalar say "m"and proceed with the calculation as gen X=sales-m if
> month>2?
>
> 3. Also Suppose I had several companies would I be able to create a vector
> of these means and use that vector to generate the values of X for each
> month>2 for each company?
>
> Any help appreciated...
> Thanks in advance
>
> Regards
> Rajesh Tharyan
>
> *
> * 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/
*
* 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/