Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS drop multiple variables indexed by tens

My question is likely stupid but I have not found an answer yet. I have a variable var index by tens : var10, var20... var90. At some point of my code I want to drop all of them.

I can do

data want(drop=var10 var20 var30 var40 var50 var60 var70 var80 var90);
    set have;
run;

I was wondering if there was a more condensed way of doing that. I know if there were variables indexed 10, 11, 12, 13... I could use

(drop=var10-90)

But as I do not have them, if I use this instruction it still does the job, but with a warning, which is not acceptable for me (I have to create programs that will be used by people with little to nothing programming knowledge, so that they will report a warning like this one).

Thanks in advance

like image 455
Anthony Martin Avatar asked Dec 06 '22 21:12

Anthony Martin


1 Answers

If the var<xx> variables are all multiples of ten, i.e. there are no other variables beginning with var, you can use the colon-operator, which acts as a wildcard, e.g.

  drop var: ; /* drop all variables beginning with 'var' */

Alternatively, you can dynamically generate a list of all the variables :

proc sql noprint ;
  select name into :VARLIST separated by ' '
  from dictionary.columns
  where libname = 'WORK'
    and memname = 'HAVE'
    and compress(name,,'ka') = 'VAR'
    and mod(input(compress(name,,'kd'),8.),10) = 0 /* convert var<xx> to xx and check divisible by 10 */
  order by name ;
quit ;

data want (drop=&VARLIST) ;
  set have ;
run ;
like image 166
Chris J Avatar answered Jan 07 '23 22:01

Chris J