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: Variables loop and deletion under certain conditions


From   Chris Parker <[email protected]>
To   [email protected]
Subject   Re: st: Variables loop and deletion under certain conditions
Date   Thu, 12 May 2011 17:53:34 +0100

Hi Nuno,

I'm sure someone else will give an eloquent explanation of why your
code doesn't work, but this code should work:

foreach varname of varlist v* {
       scalar istrue = `varname'[5]=="#ERROR"
       if istrue {
              drop `varname'
       }
}


All the best,
Chris

Chris Parker

________________________________

PhD Candidate | Management Science & Operations
London Business School | Regent's Park | London NW1 4SA | United Kingdom
Direct line +44 (0)20 7000 8816 | Email [email protected]

On Thu, May 12, 2011 at 5:29 PM, Nuno Soares <[email protected]> wrote:
>
> Hi everyone,
>
> I'm having a problem regarding a data management task. I have several
> .csv files that I need to import and treat in Stata. Each file has a
> variable number of columns (which are variables) that cannot be
> determined beforehand, and are imported into Stata as v1, v2, ...
> Also, some of these variables/columns have no data which the database
> from where they were extracted identifies with an #ERROR message in
> line 5. What I need is to delete these variables when the error
> message is present. I tried, without success,  the following piece of
> code:
>
> foreach varname of varlist v*{
>        drop `varname' if `varname'[5]=="#ERROR"
> }
>
> However Stata returns the following error code:
>
> invalid syntax
> r(198);
>
> Any ideas of why it isn't working? This should be straightforward, but
> I can't understand why it isn't working...
>
> Best wishes,
>
> Nuno
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> ______________________________________________________________________
>
> This email has been scanned by the MessageLabs Email Security System
> on behalf of the London Business School community.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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