Bookmark and Share

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: Re: generate group variables


From   R Zhang <[email protected]>
To   [email protected]
Subject   Re: st: Re: generate group variables
Date   Sun, 16 Feb 2014 17:27:21 -0500

Many Thanks to Nick and Joe !!!

On Sun, Feb 16, 2014 at 4:52 AM, Nick Cox <[email protected]> wrote:
> Also check out Kit Baum's FAQ
>
> How do you define group characteristics in your data in order to create subsets?
>
> http://www.stata.com/support/faqs/data-management/group-characteristics-for-subsets/index.html
> Nick
> [email protected]
>
>
> On 16 February 2014 03:51, Joseph Coveney <[email protected]> wrote:
>> Rochelle Zhang wrote:
>>
>> I would like to generate 36 group code based on technological
>> classification code. for example,
>>
>> for technological classification code= 8,19,71, 127, 442, 504, it will
>> be coded as groupcode=11,
>>
>> for technological classification code= 106, 118, 401, 427, it will be
>> coded as groupcode=12,
>>
>> .... etc
>>
>>
>> I just switched from sas programming to stata, hence, not very good at
>> stata coding.
>>
>> could you shed light one this?
>>
>> --------------------------------------------------------------------------------
>>
>> There are a few ways of doing this.  The one shown below uses the Stata function -inlist()- and a loop that traverses a series of strings containing the lists of classification codes that you want to group.  A separate local macro, -group_code-, is initialized before the loop, and then incremented during each pass through the loop and used to assign values to the -group_code- dataset variable.
>>
>> There might also be a user-written command that does the same thing, but I cannot remember any off the top of my head.
>>
>> Joseph Coveney
>>
>> . input int classification_code
>>
>>      classi~e
>>   1. 8
>>   2. 19
>>   3. 71
>>   4. 127
>>   5. 442
>>   6. 504
>>   7. 106
>>   8. 118
>>   9. 401
>>  10. 427
>>  11. end
>>
>> .
>> . generate byte group_code = 0
>>
>> .
>> . local group_code 11
>>
>> . foreach classification_group in ///
>>>     "8, 19, 71, 127, 442, 504" ///
>>>     "106, 118, 401, 427" {
>>   2.
>> .     replace group_code = `group_code' ///
>>>         if inlist(classification_code, `classification_group')
>>   3.     local ++group_code
>>   4.
>> . }
>> (6 real changes made)
>> (4 real changes made)
>>
>> .
>> . list , noobs sepby(group_code) abbreviate(30)
>>
>>   +----------------------------------+
>>   | classification_code   group_code |
>>   |----------------------------------|
>>   |                   8           11 |
>>   |                  19           11 |
>>   |                  71           11 |
>>   |                 127           11 |
>>   |                 442           11 |
>>   |                 504           11 |
>>   |----------------------------------|
>>   |                 106           12 |
>>   |                 118           12 |
>>   |                 401           12 |
>>   |                 427           12 |
>>   +----------------------------------+
>>
>> .
>> . exit
>>
>> end of do-file
>>
>>
>> *
>> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index