Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using values in an variable to save parts of an dataset


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Using values in an variable to save parts of an dataset
Date   Tue, 30 Apr 2013 10:42:51 -0400

note you need to preserve/restore the data in the loop and be more
careful with locals vs variables.

sysuse auto, clear
levelsof foreign, local(levs)
foreach lev in `levs' {
 preserve
   keep if foreign==`lev'
   save c:\temp\auto_`lev'
 restore
}

Best, Sergiy

On Tue, Apr 30, 2013 at 10:33 AM, 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index