DATA Step, Macro, Functions and more

how do I get the value of a variable via its name

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

how do I get the value of a variable via its name

If a condition is met, I want to call missing of a set of variables whose members depend on the condition as well.

 

My hard coded program works but I cannot automate it.

DATA TEST;

ARRAY X [4] $3 ('ANT' 'BEE' 'CAT' 'DOG'); 
N=DIM(X);
DO U=1 TO N-1;
NCOMB=COMB(N,U ); 
DO I=1 TO NCOMB;   

CALL LEXCOMB(I, U, OF X[*]);
OUTPUT;
END;
END;
NFACT=FACT(N);
DO J=1 TO NFACT;
CALL ALLPERM(J,OF X[*]);
OUTPUT;
END;
RUN;

DATA TEST2;
SET TEST;

IF U=1 THEN CALL MISSING (OF X2-X4);
IF U=2 THEN CALL MISSING (OF %SYSFUNC(CATT(X3-,X,%EVAL(3+1)))); 
IF U=3 THEN CALL MISSING (X4);
HELP=VVALUEX(CATT("X",%SYSFUNC(SUM(3,1))));

RUN;

 

The CALL MISSING part does not accept in the %eval bracket the numeric variable but complains that a char var is passed.

Thanks


Accepted Solutions
Solution
‎04-25-2018 04:16 PM
Super User
Posts: 6,935

Re: how do I get the value of a variable via its name

[ Edited ]

You're right, it definitely doesn't work for that.  DATA step statements cannot change from one observation to the next.  Whatever the statement is, it remains the same for all observations.  You might need a loop, more like:

 

array x {*} X: ;

do k=3 to u+1;

   call missing (x{k});

end;

 

View solution in original post


All Replies
Super User
Posts: 6,935

Re: how do I get the value of a variable via its name

[ Edited ]

The DATA step cannot use CAT functions to generate the code that is part of the same DATA step.  A simpler version would overcome the initial problem:

 

IF U=2 THEN CALL MISSING (OF X3-X%EVAL(3+1));

 

I'm not sure what you're trying to get into the HELP formula, but this might be a little closer:

 

help = X%eval(3+1);

 

The macro functions do not execute as the DATA step executes.  They execute before the DATA step begins, and generate the statements that become part of the DATA step code.

Contributor
Posts: 58

Re: how do I get the value of a variable via its name

Posted in reply to Astounding

Thank you.

 

But it doesn´t work neither when putting

IF U=2 THEN CALL MISSING (OF X3-X%EVAL(U+1));

 

The code deosn't like the reference to the numeric variable U

 

 

 

Solution
‎04-25-2018 04:16 PM
Super User
Posts: 6,935

Re: how do I get the value of a variable via its name

[ Edited ]

You're right, it definitely doesn't work for that.  DATA step statements cannot change from one observation to the next.  Whatever the statement is, it remains the same for all observations.  You might need a loop, more like:

 

array x {*} X: ;

do k=3 to u+1;

   call missing (x{k});

end;

 

Contributor
Posts: 58

Re: how do I get the value of a variable via its name

Posted in reply to Astounding

It has worked out. Thank you again.

 

The final code is:

 

DATA TEST;

ARRAY X [4] $3 ('ANT' 'BEE' 'CAT' 'DOG'); 
N=DIM(X);
DO U=1 TO N-1;
NCOMB=COMB(N,U ); 
DO I=1 TO NCOMB;   

CALL LEXCOMB(I, U, OF X[*]);
OUTPUT;
END;
END;
NFACT=FACT(N);
DO J=1 TO NFACT;
CALL ALLPERM(J,OF X[*]);
OUTPUT;
END;
RUN;

DATA TEST2;
SET TEST;
ARRAY X{*} X:;

DO K=2 TO DIM(X);
IF U LT K THEN CALL MISSING (X{K});
END;

HELP=VVALUEX(CATT("X",%SYSFUNC(SUM(3,1))));

RUN;

OUTPUT.png

 

Contributor
Posts: 58

Re: how do I get the value of a variable via its name

Perfect. Thanks.
If you have an idea to achieve my goal within the first permutation generating data set... Feel free to excel
Occasional Contributor
Posts: 7

Re: how do I get the value of a variable via its name

[ Edited ]

 

Or you could generate the data set wih one Data Step only, although two sets of extra temporary variables are in need.

 

data test;
   array y[4] $3. _temporary_ ('ANT' 'BEE' 'CAT' 'DOG');
   array _x[4] $3.;
   array x[4] $3.;
   drop _:;
   n = dim(y);
   do i=1 to n;
      do j=1 to comb(n, i);
         if j=1 then do k=1 to n;
            _x[k] = y[k];
         end;
         call lexcomb(j, i, of _x[*]);
         do k=1 to i;
            x[k] = _x[k];
         end;
         output;
      end;
   end;
run;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 181 views
  • 5 likes
  • 3 in conversation