BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
acordes
Rhodochrosite | Level 12

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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.

acordes
Rhodochrosite | Level 12

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

 

 

 

Astounding
PROC Star

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;

 

acordes
Rhodochrosite | Level 12

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

 

acordes
Rhodochrosite | Level 12
Perfect. Thanks.
If you have an idea to achieve my goal within the first permutation generating data set... Feel free to excel
jim_cai
Calcite | Level 5

 

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;

 

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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