Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: Variables loop and deletion under certain conditions

From   Eric Booth <>
To   "<>" <>
Subject   Re: st: Variables loop and deletion under certain conditions
Date   Thu, 12 May 2011 16:51:17 +0000


Revisit -help drop- documentation. The -drop- syntax allows for a varlist OR an if condition (which drops observations, not variables), not both.  
You could use the -if- before hand like this:
sysuse auto, clear
tostring price mpg, force replace
keep price mpg make
foreach varname of varlist make price mpg {
if `varname'[5]=="Buick Electra" drop `varname' 
See -help ifcmd- for more on this.

- Eric

Eric A. Booth
Public Policy Research Institute
Texas A&M University

On May 12, 2011, at 11:29 AM, Nuno Soares 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:
> *
> *
> *

*   For searches and help try:

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