Date: Fri, 24 Feb 2012 07:50:32 -0800
Reply-To: "Terjeson, Mark" <Mterjeson@RUSSELL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Terjeson, Mark" <Mterjeson@RUSSELL.COM>
Subject: Re: Display no observations
In-Reply-To: <CABOqZsxFY_bSree4gyVPD_4Lp9OsRripgEKBf1GFgjDc5WsHGg@mail.gmail.com>
Content-Type: text/plain; charset="us-ascii"
Hi sas_quest,
Let's cover two things, 1) the macro world,
and, 2) getting a dataset variable value into
a macro variable.
Be careful not to fall into the misconception
that SAS Macro is a separate object or a separate
language or a separate environment. SAS Macro
is merely some goodies to tell the pre-processor
to modify your source code text before the source
code gets run through the runtime compiler. It
is effectively a way to trigger text substitution.
You can invision that after the pre-processing
macro parser resolves all of the text substition,
just as if you had editted your own text yourself,
it then sends the now modified source code text
to the runtime compiler. Another way to look at
it is that during that initial macro parsing the
macro parser basically ignores all of the regular
code and only looks for those macro symbols and
tokens that trigger some text manipulation or
text substitution. So is it two layers, well sort
of, only in the aspect that the first pass is only
looking for macro goodies, and the last pass just
compiles all the resultant modified code text.
You will notice that I don't say first pass and
second pass, I said first pass and last pass.
One of the most powerful capabilities of the SAS
Macro text substitution feature is that you can
have effectively layers of resolution. i.e. the
macro parser will pass through and resolve tokens
as many passes as necessary until they are all
resolved, and then, the last pass is the actual
compiler. e.g. you can have one layer of macro
tokens actually build the next layer of macro
tokens and then the next subsequent pass through
the macro parser of that intermediately-resolved
text then gets the next layer of macro tokens
resolved.
Long story short macro variables and datastep
variables don't really talk to each other in
any way. All of the macro tokens and symbols
get resolved while ignoring all datastep code
and variables down to the resultant modified
source code and now you don't have any macro
goodies left, you just have datastep and proc
code that is ready for the compiler.
However, SAS has provided a few functions that
make it seem like you can talk back-n-forth,
in just a couple scenarios and you basically
grasp the where and the why by focusing on the
"timing" of when they get resolved, then you
can understand the few slivers of time when
SAS can make it seem like it is passing things
back and forth. That aspect is very timing
oriented.
So now on to answer your second question.
How to get a datastep variable value into a
macro variable. (macro variables are always
only string text by the way).
data outgoingdataset;
set incomingdataset;
if certaincondition then
call symput('MyMacroVar',mydatasetvariable);
run;
The 2nd argument can be any string expression.
You can picture that
call symput('MyMacroVar',mydatasetvariable);
is the same as
%let MyMacroVar=mydatasetvariablecontents;
with the following difference. Since macro variables
and datastep variables really can't talk to each
other, the MyMacroVar macro variable, even though you
have set the value, it is not available for use until
after that datastep is over. The next datastep or proc
or other code can then make use of it. (i.e. one of
the timing issues we spoke about)
Note: since there is only one macro variable, if your
datastep loads the contents during more than one
observations, then it is being overwritten, thus after
the datastep is over and you can now use that macro
variable it is only going to contain the last value
loaded into it.
SAS Macro is one of the best things since sliced bread,
and one of the most powerful, but it shines when you
understand to use it sparingly.
SAS Macro really meliorates your SAS Programming Code!
Hope this is helpful.
Mark Terjeson
Investment Business Intelligence
Investment Management & Research
Russell Investments
206-505-2367
Russell
Global Leaders in Multi-Manager Investing
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of sas quest
Sent: Friday, February 24, 2012 6:58 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Display no observations
Hi,
I want to assign a value to a variable as "No observations" from an
empty dataset that changes its variables each time it is run. The
dataset is inside a complex macro.
I know many ways to create it and display it but inside the macro i
find it difficult................
i need something like this:
data abc;
set xyz;
/*code needed here to create var*/
&var="No observations";
run;
The above macro variable "var" should be chosen from the datset xyz
which changes its variable names dynamically.
Thanks in advance