Date: Sat, 5 Nov 2005 19:56:54 +0000
Reply-To: iw1junk@COMCAST.NET
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1junk@COMCAST.NET>
Subject: Re: using macro to assign a Global Variable by condition
Alice,
Your question has been answered with
1) declare the variable global
2) improve the design and generate the value
I would raise another issue. The level of your question and the
design illustrated suggest that you have much to learn. You might
go to http://www.lexjansen.com/sugi/index.htm and search with the
words "macro" and "beginning". I found 263 entries, many of
which are good. If you add "whitlock" there are only 22, but I
agree with many of them.
Since you have chosen to call your program PROPS.MAC, you have
cut yourself off from the system's ability to automatically
compile macros located in an autocall library. Personally, I
consider this a gross mistake, but then I had to work for several
years in a department where the extension MAC was the rule.
You told us why your code does not work with a simple example, so
you got answers; but you did not tell us what your problem is, so
you cut yourself off from more useful help. For example, we
cannot know whether a macro is the proper way to store the
relationship between SP and FIRSTWK, since we do not know the
size of this relation or its purpose. Even if a macro is the
right way to store this information, we cannot know whether a
global macro variable is wise or it would be better to generate a
value, although experience suggests the latter.
At the very least I would separate %IF statements by %ELSE and
allow an appropriate default value. Perhaps the following is a
better structure
%macro firstwk ( sp ) ;
%local rel x ;
%let rel = AB60 CD36 ... YZ92 ;
%let x = %index(&rel,%upcase(&sp)) ;
%if &x %then
%substr(&rel,&x+2,2) ;
%else
%put ERROR: (firstwk) illegal parm value SP=&sp ;
%mend firstwk ;
%put >>%firstwk(ab)<< ;
If the list is long then perhaps David is right and
proc format lib = library ;
invalue firstwk (upcase)
"AB" = 60
"CD" = 36
other = .E
;
run ;
%put %sysfunc(inputn(ab,firstwk)) ;
would be better.
If you want the best of help, both example and a description of
the problem are needed.
Ian
=========
Date: Fri, 4 Nov 2005 13:44:38 -0800
Reply-To: Alice <wuhaihong@GMAIL.COM>
Sender: "SAS(r) Discussion"
From: Alice <wuhaihong@GMAIL.COM>
Organization: http://groups.google.com
Subject: using macro to assign a Global Variable by condition
Comments: To: sas-l
Content-Type: text/plain; charset="iso-8859-1"
My task is very simple here.
I want to assign a Global Variable, firstwk, according to the value of
SP.
I tried following code, it wouldn't work.
I want to know WHY it doesn't work and HOW to modify it to make it
work.
***Program: PROPS.mac;
%macro props;
%if &sp.=AB %then %let firstwk = 60;
%if &sp.=CD %then %let firstwk = 36;
%mend props;
***Program: test.mac
%macro x;
%include "c:\sas\props.mac";
%let sp=AB;
%props;
%put firstwk=&firstwk.;
%mend x;
%x;