Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Andreas Dall Frøseth <Andreas.Froseth@stud.nhh.no> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | SV: st: Using values in an variable to save parts of an dataset |
Date | Thu, 2 May 2013 07:58:35 +0000 |
It looks like I need some help with this code again... After running this a couple of times, for different restrictions, I experience some difficulties. In addition to -keep-ing if industryid==`industryid', I wish to drop if the value in the variable "length" is less than a certain value. But, for some industryids there are no companies listed with a "length" over lets say 8, which leads to an error, and the do-file stops. How can I make the code ignore those industries, and keep on splitting my dataset? -Andreas ________________________________________ Fra: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] på vegne av Andreas Dall Frøseth [Andreas.Froseth@stud.nhh.no] Sendt: 30. april 2013 16:57 Til: statalist@hsphsun2.harvard.edu Emne: SV: st: Using values in an variable to save parts of an dataset I tried the approach where you exploited a feature of -use-, and it seems to work just fine. Thank you. -Andreas ________________________________________ Fra: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] på vegne av Nick Cox [njcoxstata@gmail.com] Sendt: 30. april 2013 16:44 Til: statalist@hsphsun2.harvard.edu Emne: Re: st: Using values in an variable to save parts of an dataset Your local references are the wrong way round. foreach industryid in `industryids' { keep if industryid==`industryid' save industry_`industryid' } You want each statement inside the loop to take on each individual value. This is all assuming that -industryid- is numeric. However, even with this fixed your loop won't work. Second time round, all you have in memory is the first subset resulting from -keep-. But then you can exploit a feature of -use-: levelsof industryid, local(industryids) foreach industryid in `industryids' { use mydata if industryid==`industryid', clear save industry_`industryid' } Nick njcoxstata@gmail.com On 30 April 2013 15:33, Andreas Dall Frøseth <Andreas.Froseth@stud.nhh.no> wrote: > Dear all. > > I'm trying to divide my dataset into pieces based on the values in an variable. My set is an large panel data, containing a number of companies. Each company has a value in the variable "industryid", which allows me to identify what industry it operates in. > I am now trying to divide this large dataset into smaller sets for each single industry. > > The reason why I'm struggling is that I wish to apply this separation for a number of different sets, which might contain a different amount of industries, without having to identify the values in the industry-variable myself. > I have tried to make a macro with the values using the "levelsof" command, and then apply it with: > > foreach industryid in `industryids' { > keep if industryid==`industryids' > save industry_`industryids' > } > > > But this runs back as "invalid '10'". > > Any suggestions on how to do this? > Forgive me if this is way to basic... > > Your feedback will be appreciated. > > -Andreas > * > * 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/