|
Hello everybody,
Again I need your help with a program. Probably it is not too complicated
and perhaps the approach I am using is not the correct, so I would really
appreciate any help/suggestion (I apologize before hand for the long email
below, I just want to make it clear so people can help me better.thanks!)
I am trying to write a code where the user specifies the variable names of
one or more equations, then I use those variable names to do things like
proc means, etc. Here is an example:
I am using %let (within a macro) to assign a name to a set of variable
names:
%macro test;
%GLOBAL _LHS_11 _LHS_21 _LHS_31 _RHS_11 _RHS_21 _RHS_31 _M_ _S_;
%let _LHS_11_ = yyy; *the user will input these variable names, see example
below;
%let _RHS_11_=xxx;
* If having more equations then I have %let _LHS_21_=zzz, %let _RHS_21_=vvv;
and so on);
% let _M_=_M;
% let _S_=_S; *These are the suffix that will be added to the variable
names in the program:;
*some data manipulation here;
Data a; set test; run;
Proc Means DATA=A MEAN; VAR &_RHS_11 &_LHS_11; BY TNO; OUTPUT OUT=B
MEAN=&_RHS_11&_M_ &_LHS_11&_M_
SUM=&_RHS_11&_S_ &_LHS_11&_S_;
RUN;
*more lines and more procedure;
%mend MKTE;
%MKTE;
Ok, then let's assume that the user has yyy=P1 and xxx=Q1 (so P1 and Q1 are
two variable names found in the A data set) then the SAS program would do
this:
%GLOBAL _LHS_11 _LHS_21 _LHS_31 _RHS_11 _RHS_21 _RHS_31 _M_ _S_;
%let _LHS_11_ = P1; *user writes P1 and Q1
%let _RHS_11_=Q1;
% let _M_=_M;
% let _S_=_S; *These are the suffix that will be added to the variable
names in the program:;
*some data manipulation here;
Data a; set test; run;
Proc Means DATA=A MEAN; VAR P1 Q1; BY TNO; OUTPUT OUT=B
MEAN=P1_M Q1_M
SUM=P1_S Q1_S; *NOTE THAT SAS replace the variable names internally;
RUN;
*more lines;
%mend MKTE;
%MKTE;
So far it works fine, you can see the goal is to run the proc means and
output a file adding "_M" and a "_S" (for mean and sum) to the variable
names. Now here is the problem with this approach, if I have TWO or more
variable names then this doesn't work: For example if yyy=P1 P2 (so I have
two variables P1 and P2), since the value of yyy is just a text "P1 P2",
when SAS reads it (internally) in the proc means lines it would be something
like this:
Proc Means DATA=A MEAN; VAR P1 P2 Q1; BY TNO; OUTPUT OUT=B
MEAN=P1 P2_M Q1_M
SUM=P1 P2_M Q1_S; *
RUN;
You cans see that it 'writes' correctly in the first part (VAR P1 P2 Q2),
but when I need to add the _m and _s then I have the problem that it treats
P1 and P2 as one set of characters and adds at the end the _m or _s (e.g.
"P1 P2_M"). This is what is wrong.
Sorry for this long email, but I thought I better explain in detail my
problem so people can help me with this.
Thanks in advance,
Roberto
|