|
On Jul 9, 6:39 pm, c...@oview.co.uk wrote:
> Dirk,
>
> You're mixing data step code and macro code, which is almost always a
> mistake. You can't make a %let statement (macro code) conditional
> using an if statement (data step code). Try running your macro with
> "options mprint" turned on, and look at the code that your macro
> generates - you'll see it's not at all what you want. Use Elvis to
> make viewing the MPRINT code easier (Views -> MPRINT only).
>
> Also, you don't need the first double ampersand in the %let statement
> - one is enough - but as the %let shouldn't be there at all, it's not
> worth fixing.
>
> To modify a macro variable's value using data step code, look up CALL
> SYMPUT and SYMGET. This would be a first step:
>
> if &&var&i>0 then call symput('keep1', trim(symget('keep1')) || '
> &&var&i');
>
> but that would still be a very sub-optimal solution.
>
> Chris.
> --------------------------------------------------------
> Elvis SAS Log Analyser -http://www.oview.co.uk/elvis
> Recover runnable SAS code from your log's MPRINT lines.
> --------------------------------------------------------
>
> On 9 Jul, 13:23, Dirk Nachbar <dirk...@googlemail.com> wrote:
>
> > Hi
>
> > I want to create a macro variable with all variables names whose only
> > value is bigger than zero (the input is from proc means). Below is the
> > macro, unfortunately at the moment all possible variables get selected
> > (ie keep=var1 to var&num_vars), although not all are above 0. Any
> > ideas?
>
> > &&var&i is pre-defined.
>
> > Dirk
>
> > %macro keep;
> > %let keep1=;
> > data _null_;
> > set sums;
> > %do i=1 %to &num_vars;
> > /*create a string with the variables to keep*/
> > if &&var&i>0 then %let keep1=&&keep1 &&var&i;;
>
> > %end;
> > run;
>
> > data fggf;
> > set gffdfdas (keep=ddfs &&keep1);
> > run;
> > %mend;
> > %keep;
I propose the following solution to your problem:
Aim: is to get the variable names into a macro variable whose values
are greater than zero.
Assuming your data from proc means gives you something like this
dataset ONE
DATA ONE;
INPUT var1 var2 var3;
DATALINES;
1 -3 34
;
RUN;
So you would expect to get var1 and var3 variables into the macro
variable MVAR
so first I transpose the dataset ONE
PROC TRANSPOSE DATA=ONE OUT=OUTDSN;
RUN;
PROC SQL;
SELECT _NAME_;
INTO :MVAR SEPARATED BY ' ' ;
FROM OUTDSN
WHERE COL1 GT 0;
QUIT;
%PUT &MVAR.;
I hope this answers your query..
|