On Tue, 2 Aug 2011, Matteo Chinazzi wrote:
I would like to know how to do two specific operations which I am not
able
to perform in a proper way:
1) delete variables using loop/for constructs
2) remove variables which contain only missing values and/or have less than
"N" valid observations
1) As far as the first problem is concerned, I partially solved it by
writing a file using outfile and printf but I guess it is not the most
efficient way to do it. That is, for the time being I solved it in the
following way:
loop for (i=1; i<100; i+=1)
> outfile --append foo.inp
> printf "delete %d\n", $i
> outfile --close
> endloop
> run foo.inp
However, I cannot directly use something like this:
loop for (i=1; i<100; i+=1)
> delete $i
> endloop
because I receive the following error message: "You cannot delete series in
this context" .
2) Besides the problem at point 1), I would like to know if there is a way
to delete variables using some sort of condition upon the number of
observations they contain. That is, what I have in mind is something like
this (N is a scalar):
loop for (v=1; v<100; v+=1)
> if (sum(missing($v))<N)
> delete $v
> endif
> endloop
However, I cannot do it because if I run "missing($v)" inside a loop it
interprets "$v" as a number and not as a reference to a variable.
You can use the varname() function to get the name of a variable
specified by its ID number.
Notice also that, in my case, I cannot build a list containing all
the (string) names of all the variables since I get an error
(probably because Gretl cannot handle two thousands strings... I
guess..).
The limitation is a maximum command-line length of 8192 bytes. Other
than that, you can construct a list of variables meeting some
criterion via a loop, then delete all variables in the list once the
loop terminates. For example, the following will delete variables
numbered 1 through 100 that have less than 20 valid observations;
<hansl>
list delvars = null
loop i=1..100
string vname = varname(i)
if sum(ok(@vname)) < 20
delvars += @vname
endif
endloop
delete delvars
</hansl>
Allin Cottrell