| Date: | Fri, 9 Dec 2011 02:31:51 -0500 |
| Reply-To: | Søren Lassen <s.lassen@POST.TELE.DK> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Søren Lassen <s.lassen@POST.TELE.DK> |
| Subject: | Re: a macro checking invalid variable name |
|
| Content-Type: | text/plain; charset=ISO-8859-1 |
Max,
What happens is this: as you placed the macro call before the RUN
statement in the datastep, the macro gets executed before the datastep.
So, the parameter &value has whatever was assigned to it before the
datastep, not the SYMPUT-ed value.
Likewise, in %bquote(trim(varname)) the %BQUOTE has no effect, what gets
%BQUOTE-d is the text "trim(varname)".
I think that what you want to accomplish can be done by
%macro checkvarname(value);
%let valid=%sysfunc(nvalid(&value,v7));
%if &valid=0 %then
%do;
%let position=%sysfunc(notname(&value));
%put ***The first invalid character of %upcase(&value) is in position:;
%put &position;
%end;
%else
%put ***%upcase(&value) is a valid variable name.;
%put;
%put;
%mend checkvarname;
data _null_;
length varname $ 39;
input varname $ 1-39;
*the purpose of using %bquote is to preserve blanks at the beginning of
literals;
call execute(catt('%checkvarname(%str(',varname,'))'));
cards;
valid_name
valid_name
invalid name
book_sales_results_for_past_five_years!
;run;
I also changed "valid" to "&valid" in the macro.
There is a potential problem with your macro, though: side effects.
As the macro variables VALID and POSITION are not declared %LOCAL, they
will overwrite macro variables of the same name defined in the calling
environment (if defined there). This can be remedied by using %LOCAL,
or by dispensing of the variables, which are only used once, anyway:
%macro checkvarname(value);
%if %sysfunc(nvalid(&value,v7))=0 %then
%do;
%put ***The first invalid character of %upcase(&value) is in position:;
%put %sysfunc(notname(&value));
%end;
%else
%put ***%upcase(&value) is a valid variable name.;
%put;
%put;
%mend checkvarname;
Regards,
Søren
On Thu, 8 Dec 2011 23:58:49 -0500, bbser 2009 <bbser2009@GMAIL.COM> wrote:
>Greetings!
>
>The code below is suppose to check if a given literal can be used as a
>variable name.
>I would like to check the four literals one at a time in the data step.
>Only the first two are suppose to be valid.
>But after running the code, I only got one comment in the log about the
last
>long literal (39 characters long), which said the literal is valid,
>apparently contrary to the name criterion.
>What am I missing here? In addition, do you think I am using %bquote
>correctly?
>Please help. Thank you very much in advance!
>
>Regards, Max
>(Maaxx)
>
>%macro checkvarname(value);
>%let valid=%sysfunc(nvalid(&value,v7));
>%if valid=0 %then
> %do;
> %let position=%sysfunc(notname(&value));
> %put ***The first invalid character of %upcase(&value) is in position:;
> %put &position;
> %end;
>%else
> %put ***%upcase(&value) is a valid variable name.;
> %put;
> %put;
>%mend checkvarname;
>
>data _null_;
>length varname $ 39;
>input varname $ 1-39;
>*the purpose of using %bquote is to preserve blanks at the beginning of
>literals;
>call symput('value',%bquote(trim(varname)));
>%checkvarname(&value)
>cards;
>valid_name
> valid_name
>invalid name
>book_sales_results_for_past_five_years!
>;
|