Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Command for selecting values
From
Clarice Martins <[email protected]>
To
[email protected]
Subject
Re: st: Command for selecting values
Date
Wed, 30 Oct 2013 19:39:20 -0200
It works!
Thank you Nick!!
(Reading your paper on comparing Data before asking any more questions!!!)
Amazing, this Stata world of programming!!!
Regards,
Clarice
On Oct 30, 2013, at 5:14 PM, Nick Cox wrote:
> Your previous question was about cross-checking within companies.
>
> But your earlier definitions imply that you calculated quintiles
> separately for each value of -yrmonth- in which case your calculation
> might be
>
> bysort co_id yrmonth (quintile) : replace quintile = quintile[1]
>
>
> Nick
> [email protected]
>
>
> On 30 October 2013 18:47, Clarice Martins <[email protected]> wrote:
>> Dear Nick and Group,
>>
>> Reviewing the dataset, suggestion code and other material, I noticed that:
>>
>> - it is necessary to -replace- quintile data to other observations, respecting variable co_id (which the command did perfectly), but also taking into consideration the variable yrmonth.
>>
>> - i also figured out that my sample dataset was not very representative of the reality, since it was too simplistic (my apologies...!!!!!)
>>
>> - i created below new sample data, and tested now the command proposed created same output in main dataset and sample data ==> Results were the same in both
>>
>> *-------output from Stata----------------
>> clear
>> input byte co_id str4 ticker str10 rtype str10 yrmonth double return byte quintile
>>
>> co_id ticker rtype yrmonth return quintile
>> 1. 1 ABCB formation jun2000 0.01 1
>> 2. 1 ABCB formation jul2000 0.01 2
>> 3. 1 ABCB formation aug2000 0.01 5
>> 4. 1 ABCB buysell jul2000 0.01 .
>> 5. 1 ABCB buysell aug2000 0.01 .
>> 6. 1 ABCB buysell sep2000 0.01 .
>> 7. 1 ABCB holding jan2001 0.01 .
>> 8. 1 ABCB holding feb2001 0.01 .
>> 9. 1 ABCB holding mar2001 0.01 .
>> 10. 2 AEDU formation jun2000 0.01 2
>> 11. 2 AEDU formation jul2000 0.01 1
>> 12. 2 AEDU formation aug2000 0.01 3
>> 13. 2 AEDU buysell jul2000 0.01 .
>> 14. 2 AEDU buysell aug2000 0.01 .
>> 15. 2 AEDU buysell sep2000 0.01 .
>> 16. 2 AEDU holding jan2001 0.01 .
>> 17. 2 AEDU holding feb2001 0.01 .
>> 18. 2 AEDU holding mar2001 0.01 .
>> 19. 3 AMBV formation jun2000 0.01 3
>> 20. 3 AMBV formation jul2000 0.01 4
>> 21. 3 AMBV formation aug2000 0.01 5
>> 22. 3 AMBV buysell jul2000 0.01 .
>> 23. 3 AMBV buysell aug2000 0.01 .
>> 24. 3 AMBV buysell sep2000 0.01 .
>> 25. 3 AMBV holding jan2001 0.01 .
>> 26. 3 AMBV holding feb2001 0.01 .
>> 27. 3 AMBV holding mar2001 0.01 .
>> 28. 4 FFFF formation jun2000 0.01 4
>> 29. 4 FFFF formation jul2000 0.01 3
>> 30. 4 FFFF formation aug2000 0.01 5
>> 31. 4 FFFF buysell jul2000 0.01 .
>> 32. 4 FFFF buysell aug2000 0.01 .
>> 33. 4 FFFF buysell sep2000 0.01 .
>> 34. 4 FFFF holding jan2001 0.01 .
>> 35. 4 FFFF holding feb2001 0.01 .
>> 36. 4 FFFF holding mar2001 0.01 .
>> 37. 5 BBBB formation jun2000 0.01 4
>> 38. 5 BBBB formation jul2000 0.01 2
>> 39. 5 BBBB formation aug2000 0.01 1
>> 40. 5 BBBB buysell jul2000 0.01 .
>> 41. 5 BBBB buysell aug2000 0.01 .
>> 42. 5 BBBB buysell sep2000 0.01 .
>> 43. 5 BBBB holding jan2001 0.01 .
>> 44. 5 BBBB holding feb2001 0.01 .
>> 45. 5 BBBB holding mar2001 0.01 .
>> 46. 6 MMMM formation jun2000 0.01 .
>> 47. 6 MMMM formation jul2000 0.01 .
>> 48. 6 MMMM formation aug2000 0.01 .
>> 49. 6 MMMM buysell jul2000 0.01 .
>> 50. 6 MMMM buysell aug2000 0.01 .
>> 51. 6 MMMM buysell sep2000 0.01 .
>> 52. 6 MMMM holding jan2001 0.01 .
>> 53. 6 MMMM holding feb2001 0.01 .
>> 54. 6 MMMM holding mar2001 0.01 .
>> 55. end
>>
>>
>>
>> . tabulate quintile, missing
>>
>> quintile | Freq. Percent Cum.
>> ------------+-----------------------------------
>> 1 | 3 5.56 5.56
>> 2 | 3 5.56 11.11
>> 3 | 3 5.56 16.67
>> 4 | 3 5.56 22.22
>> 5 | 3 5.56 27.78
>> . | 39 72.22 100.00
>> ------------+-----------------------------------
>> Total | 54 100.00
>>
>>
>>
>> . bysort co_id (quintile) : replace quintile = quintile[1]
>> (40 real changes made)
>>
>> .
>>
>>
>> . tabulate quintile, missing
>>
>> quintile | Freq. Percent Cum.
>> ------------+-----------------------------------
>> 1 | 27 50.00 50.00
>> 3 | 18 33.33 83.33
>> . | 9 16.67 100.00
>> ------------+-----------------------------------
>> Total | 54 100.00
>>
>> .
>>
>> - i do need to keep the 2, 4, and 5 as well,
>>
>>
>> Thanks again!!!! Learning a lot... in this group!!!
>>
>> Clarice
>>
>>
>>
>> On Oct 30, 2013, at 11:32 AM, Nick Cox wrote:
>>
>>> . bysort co_id (quintile) : replace quintile = quintile[1]
>>>
>>> lets you "spread" the non-missing values of -quintile- to all
>>> observations for each company. You can then select appropriate
>>> companies freely.
>>>
>>> For an introductory review of several tricks in this territory, see
>>>
>>> SJ-11-2 dm0055 . . . . . . . . . . . . . . Speaking Stata: Compared with ...
>>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
>>> Q2/11 SJ 11(2):305--314 (no commands)
>>> reviews techniques for relating values to values in other
>>> observations
>>>
>>> Nick
>>> [email protected]
>>>
>>>
>>> On 30 October 2013 13:04, Clarice Martins <[email protected]> wrote:
>>>
>>>> I am using Stata 12.1 for Mac.
>>>>
>>>> My problem is:
>>>> - within a dataset (with the structure below), I need to -if rtype=="formation" & yrmonth == "`lev'" & quintile==1- , pick values from -rtype==buysell- but from same companies that are in the -if- selection.
>>>>
>>>> I thought of something like this for a code:
>>>>
>>>> *-------begin code------
>>>> gen P1_sell = .
>>>> levelsof yrmonth, local(levs)
>>>>
>>>>
>>>> foreach lev of local levs {
>>>> egen OK = anymatch(co_id) values (if rtype=="formation" & yrmonth == "`lev'" & quintile==1) //na aceita if ai dentro
>>>> replace P1_sell = return if rtype=="buysell" & yrmonth == "`lev'" & co_id = anymatch(OK)
>>>> drop temp
>>>> }
>>>> *-------end code------------
>>>>
>>>> But I know there is something missing to tell the code to pick values from other observations. I have been researching similar cases on the net, but no luck yet.
>>>>
>>>> Can anyone give me a hint???
>>>>
>>>> Thank you very much,
>>>> Clarice
>>>>
>>>> *----dataset sample----
>>>> clear
>>>> input byte co_id str4 ticker str10 rtype str10 yrmonth double return byte quintile
>>>> 1 ABCB formation jun2000 0.01 1
>>>> 1 ABCB formation jul2000 0.01 1
>>>> 1 ABCB formation aug2000 0.01 1
>>>> 1 ABCB buysell jul2000 0.01 .
>>>> 1 ABCB buysell aug2000 0.01 .
>>>> 1 ABCB buysell sep2000 0.01 .
>>>> 1 ABCB holding jan2001 0.01 .
>>>> 1 ABCB holding feb2001 0.01 .
>>>> 1 ABCB holding mar2001 0.01 .
>>>> 2 AEDU formation jun2000 0.01 2
>>>> 2 AEDU formation jul2000 0.01 2
>>>> 2 AEDU formation aug2000 0.01 2
>>>> 2 AEDU buysell jul2000 0.01 .
>>>> 2 AEDU buysell aug2000 0.01 .
>>>> 2 AEDU buysell sep2000 0.01 .
>>>> 2 AEDU holding jan2001 0.01 .
>>>> 2 AEDU holding feb2001 0.01 .
>>>> 2 AEDU holding mar2001 0.01 .
>>>> 3 AMBV formation jun2000 0.01 3
>>>> 3 AMBV formation jul2000 0.01 3
>>>> 3 AMBV formation aug2000 0.01 3
>>>> 3 AMBV buysell jul2000 0.01 .
>>>> 3 AMBV buysell aug2000 0.01 .
>>>> 3 AMBV buysell sep2000 0.01 .
>>>> 3 AMBV holding jan2001 0.01 .
>>>> 3 AMBV holding feb2001 0.01 .
>>>> 3 AMBV holding mar2001 0.01 .
>>>> 4 FFFF formation jun2000 0.01 4
>>>> 4 FFFF formation jul2000 0.01 4
>>>> 4 FFFF formation aug2000 0.01 4
>>>> 4 FFFF buysell jul2000 0.01 .
>>>> 4 FFFF buysell aug2000 0.01 .
>>>> 4 FFFF buysell sep2000 0.01 .
>>>> 4 FFFF holding jan2001 0.01 .
>>>> 4 FFFF holding feb2001 0.01 .
>>>> 4 FFFF holding mar2001 0.01 .
>>>> 5 BBBB formation jun2000 0.01 5
>>>> 5 BBBB formation jul2000 0.01 5
>>>> 5 BBBB formation aug2000 0.01 5
>>>> 5 BBBB buysell jul2000 0.01 .
>>>> 5 BBBB buysell aug2000 0.01 .
>>>> 5 BBBB buysell sep2000 0.01 .
>>>> 5 BBBB holding jan2001 0.01 .
>>>> 5 BBBB holding feb2001 0.01 .
>>>> 5 BBBB holding mar2001 0.01 .
>>>> 6 MMMM formation jun2000 0.01 .
>>>> 6 MMMM formation jul2000 0.01 .
>>>> 6 MMMM formation aug2000 0.01 .
>>>> 6 MMMM buysell jul2000 0.01 .
>>>> 6 MMMM buysell aug2000 0.01 .
>>>> 6 MMMM buysell sep2000 0.01 .
>>>> 6 MMMM holding jan2001 0.01 .
>>>> 6 MMMM holding feb2001 0.01 .
>>>> 6 MMMM holding mar2001 0.01 .
>>>> end
>>>> *-------end--------
>>>>
>>> *
>>> * For searches and help try:
>>> * http://www.stata.com/help.cgi?search
>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>> * http://www.ats.ucla.edu/stat/stata/
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/