Date: Thu, 9 Dec 2004 19:59:35 +0000
Reply-To: iw1junk@COMCAST.NET
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1junk@COMCAST.NET>
Subject: Re: How to define a global determistic variable without using
Macro
Fred,
You haven't made clear what your objection to macro is. All of
the suggestions I have seen so far involve some use of macro and
may limit your use of the constant.
If your objection is that macro variables are easily changed,
then you might consider something like
%macro g1 ; hello %mend ;
data _null_ ; put "%g1" ; run ;
Of course one can still redefine macros, but one is less likely
to change macro definitions than variable values. This could be
improved to house all global constants and take a parameter to
specify which one is returned. In fact this is how I usually avoid
global macro variables.
If you really want to avoid all SAS macro tools then you could
use the following idea.
/* code to process program on CARDS4
using global constants g1 g2
*/
filename temp temp ;
data _null_ ;
/* global constants - case of names must match code */
g1 = 5 ;
g2 = "Hello" ;
Length line $ 200 ;
input line $char80. ;
do while ( index ( line, "#g1" ) > 0 ) ;
line = tranwrd ( line , "#g1" , trim(put(g1,best32. -l))) ;
end ;
do while ( index ( line, "#g2" ) > 0 ) ;
line = tranwrd ( line , "#g2" , trim(left(g2))) ;
end ;
file temp ;
put line ;
cards4 ;
/* this is the program */
data w#g1 ;
x = #g1 ;
y = "#g2" ;
put _all_ ;
run ;
title "#g2: Data set is w#g1" ;
proc print data = w#g1 ;
run ;
;;;;
/* now execute the processed program */
%inc temp / source2 ;
In a somewhat more general and realistic setting you would use
fileref other than CARDS4 for your "large program". At the
expense of more complexity you can design the above step to
accept global constants announced as such within the program to
be executed. This would allow the use of an arbitrary list of
global constants and more stable processing code. (I actually
did this as a Coder's Corner paper for NESUG some years ago.)
Of course, I have simply implemented a crude macro processor so I
haven't escaped the concept of macro (i.e. preprocessing code), I
have merely avoided all use of the SAS macro facility. On the
other hand, the concept of global constant is not built into SAS
and I very much doubt that one can achieve it in SAS without some
form of preprocessing SAS code. So what is your objection to using
macro?
Ian_Whitlock@comcast.net
===================================
Date: Wed, 8 Dec 2004 14:14:00 -0500
Reply-To: Fred <ieaggie2002@gmail.com>
Sender: "SAS(r) Discussion"
From: Fred <ieaggie2002@GMAIL.COM>
Subject: How to define a global determistic variable without
using Macro
Content-Type: text/plain; charset=US-ASCII
Hi, all
I wrote a long SAS code, and after that I realized I need
to define a global determistic variable to replace a
constant value in the code.
However, I dont want to use MACRO to define this variable.
For example, I want the code to look like:
myvariable = constant; * this constant can be changed if needed.
data xx;
statement;
run;
other proc statements involving the reference of "myvariable";
...
So is there some easy way to add this variable assignment
command?
Thanks for your point.
Fred