| Date: | Tue, 27 Jul 2004 16:16:17 -0400 |
| Reply-To: | Matthew Karafa <mkarafa@BIO.RI.CCF.ORG> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
|
| From: | Matthew Karafa <mkarafa@BIO.RI.CCF.ORG> |
| Subject: | Trying to check macro parameters for " |
|
| Content-Type: | text/plain |
Ok so my colleague and I are working on a
macro that will have a parameters like any of the following
%wheremissing(ds=_LAST_, var=v1, ExtraMissing = "NA" "Unk" "#");run;
%wheremissing(ds=_LAST_, var=v1, ExtraMissing = 77 88 99 );run;
%wheremissing(ds=_LAST_, var=v1, ExtraMissing = NA Unk #);run;
%wheremissing(ds=_LAST_, var=v1, ExtraMissing = "77" "88" "99");run;
etc.
Basically the trouble comes in the "ExtraMissing" parameter.
Since we are allowing (and determining) the char/num status
of v1, we have to allow "character" missing values as potential
additional missing values to SAS's . and "".
So my question is how the blazes do I look for a single " or '
in an input macro variable?? when we try %bquote(") the program
treats it as an unbalanced quote. Suggestions?
Actual macro we're working on is below if it'll help:
*======================= begin program ===========================;
%macro wheremissing(ds= , vlist= , idvars= , ExtraMissing=);
* Localize variables to safeguard against problems when
using with other macros.;
%local ds vlist idvars libname _ds_ i var vartype name
NumMiss char num ExtraMissing Defaultextramissing
temp__ var_dummy;
* Works correctly for permanent data sets. Must fix when
temporary file (below).;
%let libname = %scan(&ds, 1);
%let _ds_ = %scan(&ds, 2);
* If a temporary file, assign "work" libname and reassign
_ds_ correctly.;
%if &_ds_ = %then %do;
%let _ds_ = &libname;
%let libname = work;
%end;
%let i=0;
%do %while (%scan(&vlist, &i+1) ^= );
%let i=%eval(&i+1) ;
%let var=%scan(&vlist , &i);
* Create new variable "vartype" that gives variable type
(i.e., character or numeric).;
proc sql noprint;
select type into : vartype
from sashelp.vcolumn
where libname=%upcase("&libname") & memname=%upcase("&_ds_") &
upcase(name)=%upcase("&var");
quit;
run;
* Created "ExtraMissing" parameter so first change extra missing
values in list to "" for character or "." for numeric in a
temporary data set so that the rest of program runs as-was.;
%if &vartype = char %then %let DefaultMissing = "";
%if &vartype = num %then %let DefaultMissing = .;
%inc "/home/common/programs/SMUG/sasautos/countvar.sas";
data temp__;
set &ds;
* Rename variable at hand so that you can change values
in "ExtraMissing" list to " " for character variables
or "." for numeric variables in "var_dummy"
while keeping "var" the same (with extra missing values
specified by "ExtraMissing") to later print out original
missing values (i.e., not those changed to " " or "." in
"var_dummy" for the program to run smoothly).;
var_dummy = &var;
%countvar(&ExtraMissing);
* Countvar returns the number of variables in the
"ExtraMissing" list and stores in "P".;
%do NumInList = 1 %to &P;
%if %bquote(%substr(%scan(&ExtraMissing, &NumInList), 1, 1)) =
%bquote(")
%then
%bquote(ExtraMiss_NoQuotes) =
%bquote(%substr(%scan(&ExtraMissing, &NumInList), 2,
%sysevalf(%length(%scan(&ExtraMissing, &NumInList)) -1)));
* Change extra missing values to "" or ".", depending on
variable type.;
if &var = %scan(&ExtraMissing, &NumInList) then
var_dummy = &DefaultMissing;
%end;
run;
* Create variable "NumMiss" that counts number of extra missing
values.;
proc sql noprint;
select count(*) into : NumMiss
from temp__
where
%if &vartype=char %then var_dummy = "";
%else %if &vartype=num %then var_dummy = .;
;
quit;
run;
* Note variable type in the log.;
%if &vartype=char %then
%put NOTE: The variable type is CHARACTER for &var in &ds..;
%if &vartype=num %then
%put NOTE: The variable type is NUMERIC for &var in &ds..;
* Print observations with missing values to output window.;
%if &NumMiss ne 0 %then %do;
* If character variable (default missing = ""),
then output following title to output window, depending on
if extra missing variables were specified.;
%if &vartype=char %then %do;
%if &ExtraMissing ne %then %do;
title "&NumMiss obs in &ds with missing values
(missing = """" %bquote(&ExtraMissing.)) of &var.";
%end;
%else %do;
title "&NumMiss obs in &ds with missing values
(missing = """") of &var.";
%end;
%end;
* If numeric variable (default missing = .), then output
following title to output window, depending on if extra
missing variables were specified.;
%if &vartype=num %then %do;
%if &ExtraMissing ne %then %do;
title "&NumMiss obs in &ds with missing values
(missing = . %bquote(&ExtraMissing.)) of &var.";
%end;
%else %do;
title "&NumMiss obs in &ds with missing values
(missing = .) of &var.";
%end;
%end;
proc print data=temp__ noobs;
var &idvars &var;
where %if &vartype=char %then var_dummy="";
%else %if &vartype=num %then var_dummy=.;
;
run;
%end;
* Note if no missing values in the log (nothing goes to output
window for these variables).;
%if &NumMiss = 0 %then %do;
* If character variable (default missing = ""), then output
following note to log, depending on if extra missing
variables were specified.;
%if &vartype=char %then %do;
%if &ExtraMissing ne %then %do;
%put NOTE: Zero obs in &ds with missing values
(missing = %bquote("") %bquote(&ExtraMissing.)) of &var..;
%end;
%else %do;
%put NOTE: Zero obs in &ds with missing values
(missing = %bquote("")) of &var..;
%end;
%end;
* If numeric variable (default missing = .), then output
following note to log, depending on if extra missing
variables were specified.;
%if &vartype=num %then %do;
%if &ExtraMissing ne %then %do;
%put NOTE: Zero obs in &ds with missing values
(missing = . %bquote(&ExtraMissing.)) of &var..;
%end;
%else %do;
%put NOTE: Zero obs in &ds with missing values
(missing = .) of &var..;
%end;
%end;
%end;
%end;
%mend; *wheremissing;
*======================= End program ===========================;
---
Matthew T. Karafa, PhD= CCF CBC - Epidemiology & Biostatistics
mkarafa@bio.ri.ccf.org= These data do not support the hypothesis.
Phone: 445-9556 = Well...The first one does, but the second
Fax : 444-8021 = and third don't, now the fourth...
= --Unknown
|