Re: Check if a Variable is Null or not

  • Follow


hi ... courtesy of stuff in a data _null_ posting a couple weeks ago

you could turn this into a macro ...

data test;
set sashelp.class;
do j=1 to 1000;
  call missing(height,name);
  if ranuni(0) le 0.005 then call missing(weight);
  if ranuni(0) le 0.0001 then call missing(age, sex);
  output;
end;
drop j;
run;

proc format;
value $ch ' ' = 'MISSING' other = 'OK';
value nm   .  = 'MISSING' other = 'OK';
run;

ods listing close;
proc freq data=test;
   ods output onewayfreqs=xyz ;
   table _all_ / missing sparse;
   format _character_ $ch. _numeric_ nm.;
run;
ods listing;

data _null_;
length variable $32 value $64 missingvars $200;
do until (done);
   set xyz end=done;
   variable = scan(table,-1);
   value    = coalescec(of F_:);
   if value eq 'MISSING' and int(percent) eq 100 then missingvars=catx(' ',missingvars,variable);
end;
call symputx('missingvars',missingvars);
run;

data test_nomiss;
set test (drop=&missingvars);
run;


so ...


%macro misanthrope (dset);
proc format;
value $ch ' ' = 'MISSING' other = 'OK';
value nm   .  = 'MISSING' other = 'OK';
run;

ods listing close;
proc freq data=&dset;
   ods output onewayfreqs=xyz ;
   table _all_ / missing;
   format _character_ $ch. _numeric_ nm.;
run;
ods listing;

data _null_;
length variable $32 value $64 missingvars $200;
do until (done);
   set xyz end=done;
   variable = scan(table,-1);
   value    = coalescec(of F_:);
   if value eq 'MISSING' and int(percent) eq 100 then missingvars=catx(' ',missingvars,variable);
end;
call symputx('missingvars',missingvars);
run;

data &dset._nomiss;
set &dset (drop=&missingvars);
run;

proc contents data=&dset._nomiss;
run;
%mend;

%misanthrope(test);

(sort of like MISS_AND_DROP)

--
Mike Zdeb
U@Albany School of Public Health
One University Place
Rensselaer, New York 12144-3456
P/518-402-6479 F/630-604-1475

> Hello Gerhard,
>
> I got what I am looking for in Old post of your's  Just was thinking is
> there Macro solution to check both Char and Numeric variables ?? and to Drop
> them.
>
> thanks
> SL
>
> ------------------------------------------------------------------------------------------------------------------
>
> I'm sure that there are much better examples in the list, but an idea for
> nums. If you need it also for chars, you might not add them but use !!
> instead. To let it not get too long, you could compress the parts. It is
> also not necessary to get it too long, because if there is something other
> than " " in the first byte, it is not all blank:
>
> data test;
>  set sashelp.class;
>  x=.;
>  y=.;
>  if sex="F" then weight=.;
> run;
>
> data _null_;
>  set test end=eof;
>  array t(*) _numeric_;
>  length list $500;
>  do i=1 to dim(t);
>    t(i)+t(i);
>  end;
>  if eof then do;
>    do i=1 to dim(t);
>   if t(i)=. then do;
>     vv=vname(t(i));
>     list=trim(list)!!" "!!compress(vname(t(i)));
>   end;
>  end;
>  call symput("list",list);
>  end;
> run;
> %put &list;
>
>
> Gerhard
>
>
> On Mon, Mar 8, 2010 at 10:48 AM, SAS_learner <proccontents@gmail.com> wrote:
>
>> Hello Gerhard,
>> Thanks for Quick response,may be example would more clear to what I am
>> asking. I am playing with Data_null_ suggestion dynamically running proc
>> report(See the code below).  Here I know for sure that Temp and N_temp are
>> missing. But is there a way I can check to see if these variables are
>> missing ( does not have any values) so I can avoid them putting them in
>> Column statement and in the define statement.   (May be I am looking at
>> wrong angle of doing this )
>>
>>
>>
>> data class;
>>   set sashelp.class;
>>      Temp = "" ;
>>    N_temp = . ;
>>   label
>>      Age='Age of Subject'
>>      Height='Height of Subject'
>>      Name='name of the subject'
>>      ;
>>   format age f8.  Height f9.1;
>>   informat age 10. name 20.;
>>   retain Comment 'This is long comment text that needs to be flowed';
>>   run;
>>
>> %macro
>>   report
>>      (
>>         data     = , /* [R] data */
>>         columns  = , /* [O] Column statement may be blank &VARS is used */
>>         vars     = , /* [R] so we don't have to figure out the name
>> of the vars in COLUMNS */
>>         format   = , /* [O] to change formats at runtime */
>>         label    = , /* [O] to change labels at runtime */
>>         width    = , /* [O] to specify field width */
>>         flow     =   /* [O] flow option */
>>         /* you could do the rest ORDER GROUP RIGHT LEFT CENTER etc.*/
>>      );
>>
>>   %if %superQ(columns) eq %then %let columns=&vars;
>>
>>   proc transpose data=&data(obs=0) out=vars(index=(_name_));
>>      var &vars;
>>      copy &vars;
>>      %sysfunc(ifc(%sysevalF(%superq(format) ne,boolean),%nrstr(format
>> &format;),%str()));
>>      %sysfunc(ifc(%sysevalF(%superq(label) ne,boolean), %nrstr(label
>> &label;),%str()));
>>      run;
>>   proc sql noprint;
>>      select _name_ into :vars SEPARATED ' '
>>         from vars;
>>      quit;
>>   data flow;
>>      format &vars;
>>      retain &flow _dummy_ 1;
>>      output;
>>      stop;
>>      call missing(of _all_);
>>      run;
>>   proc transpose data=flow
>>         out=flow(rename=(col1=_FLOW_) where=(_name_ ne '_dummy_')
>> index=(_name_))
>>         ;
>>      var _all_;
>>      run;
>>   data width;
>>      format &vars;
>>      retain &width;
>>      retain _dummy_ 0;
>>      output;
>>      call missing(of _all_);
>>      run;
>>   proc transpose data=width
>>         out=width(rename=(col1=_WIDTH_) where=(_name_ ne '_dummy_')
>> index=(_name_))
>>         ;
>>      var _all_;
>>      run;
>>   data vars;
>>      retain _DATA_ "&data";
>>      set vars;
>>      set width key=_name_/unique;
>>      if _error_ ne 0 then do;
>>         call missing(_width_);
>>         _error_=0;
>>         end;
>>      set flow key=_name_/unique;
>>      if _error_ ne 0 then do;
>>         call missing(_flow_);
>>         _error_=0;
>>         end;
>>      length _FORMAT_ $32;
>>      _format_ = vformatX(_name_);
>>
>>      run;
>>   proc print;
>>      run;
>>   data _null_;
>>      if _n_ eq 1 then set vars(keep=_data_);
>>      length ddname $8 filevar $128;
>>      rc = filename(ddname,,'TEMP');
>>      filevar=pathname(ddname);
>>      rc = filename(ddname);
>>      putlog filevar=;
>>      file dummy filevar=filevar;
>>      put 'Proc Report nowd list headline headskip data=' _data_ ';';
>>      put +3 'Columns ' "(&columns)" ';';
>>      do until(eof);
>>         set vars end=eof;
>>         put +3 'define ' _name_ ' / display ' @ ;
>>         if not missing(_width_) then put 'width=' _width_ @;
>>         if _flow_ then put ' FLOW ' @;
>>         if not missing(_format_) then put 'FORMAT=' _format_ @;
>>         if not missing(_label_) then put _label_ :$quote258.;
>>         put +(-1) ';';
>>         end;
>>      put +3 'Run;';
>>      call execute(cats('%inc',quote(strip(filevar)),';'));
>>      stop;
>>      run;
>>   %mend report;
>> options mprint=1;
>> %report
>>   (
>>      data     = class,
>>      vars     = Age Height Name Comment Temp N_temp,
>>      columns  = %nrstr('--' &vars),
>>      format   = Height f10.1,
>>      label    = age="Student's age",
>>      width    = comment 20 age 10,
>>      flow     = comment
>>   );
>>
>
0
Reply msz03 (782) 3/8/2010 7:43:24 PM


0 Replies
207 Views

(page loaded in 0.46 seconds)

Similiar Articles:













7/17/2012 4:41:37 AM


Reply: