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
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;
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.
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
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;
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;
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.