2

Is there a command or a quick way to set all the existing variables in the PDV to missing?

I have some code which runs like this:

Data example2; 
var1='A';
Var2='B';
Var3='C';
/* etc*/
output;
Var1='B';
output;
stop;
run;

once the first 'output' statement is reached I'd like to reset all the PDV variables to missing (eg var2=''; var3='';) but without having to manually declare them as such. Can anyone help?

Allan Bowe
  • 12,306
  • 19
  • 75
  • 124

2 Answers2

7

The call missing routine and the _all_ automatic variable list will do it easily

call missing(of _all_);

For example

Data example2;
var1='A';
Var2='B';
Var3='C';
output;
call missing(of _all_);
Var1='B';
output;
stop;
run;

proc print data=example2;
run;

produces

                                 The SAS System

                               Obs    var1    Var2    Var3

                                1      A       B       C
                                2      B
cmjohns
  • 4,465
  • 17
  • 21
  • I thought call missing would be useful...didn't know about the _all_ keyword...nice – Jay Corbett Jun 10 '09 at 17:12
  • There is also _character_ and _numeric_ automatic variable lists (like simonn used in his answer) if you just want to change just the character variables or just the numeric variables, respectively. Very handy! Here's a reference: http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a000695105.htm – cmjohns Jun 10 '09 at 17:55
  • This is much nicer than my solution! I didn't realise you could pass multiple variables to call missing() like that. Thanks! – Simon Nickerson Jun 11 '09 at 08:06
1

You can do it with arrays.

Here is a macro that makes everything in the PDV missing. The parameter t is to allow you to call it multiple times from a single data step.


%macro cleanpdv(t);
array __c&t{*} _character_;
array __n&t{*} _numeric_;
do __i&t=1 to dim(__c&t);
  call missing(__c&t{__i&t});
end;
do __i&t=1 to dim(__n&t);
  call missing(__n&t{__i&t});
end;
drop __i&t;
%mend;

You might use it like this:



Data example2;
var1='A';
Var2='B';
Var3='C';
/* etc*/
output;
%cleanpdv(1);
Var1='B';
output;
%cleanpdv(2);
output;
stop;
run;

which produces the following data set:

  Obs    var1    Var2    Var3

   1      A       B       C
   2      B
   3
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
  • actually simonn - I am finding myself using your solution more often than the call missing routine - as your code can be quickly modified to turn all 'missing' variables into zeros. – Allan Bowe Jun 11 '09 at 19:54