Help using Base SAS procedures

compact SAS code

Reply
Occasional Contributor
Posts: 8

compact SAS code

I have variables A-Z. Values for A-Z (26 variables) are "Y" "N".  I want to create a new variable CHECK based on A-Z. If all A-Z (26 variables) ="N", then CHECK would be "N". How can I write a short/clean code to do so instead using code like the following? 

if A="N" and B="N" and C ="N" and D ="N" and E="N"......and Z="N" then CHECK="N"

 

Is there a short cut for above redundant code? 

thanks. 

 

Xiaoyi

Trusted Advisor
Posts: 1,360

Re: compact SAS code

[ Edited ]

data want;

 set have;

      array yn {26} a -- z;  /* better rename A-Z into q1-q26 */

     check = 'N';

    do i=1 to dim(yn);

        if yn(i) = 'Y' then check = 'Y';

  end;

run;

Super User
Super User
Posts: 6,497

Re: compact SAS code

[ Edited ]

Are you sure every variables is either N or Y.  That is there are no blank variables?

 

if whichc('Y', of A b C d E f G h I j K l M n O p Q r S t U v W x Y z) then check='Y';
else check='N';

It would be even easier if you know that all 26 varaibles as next to each other in the data vector. Then you could use variable list like A--Z instead of having to type all 26 variable names.

 

You could also look at using CATS() function to generate the 26 character string that has all of the value and then test that.

Super User
Posts: 5,071

Re: compact SAS code

If your variables are created in sequence so that this list specifies the exact right set of variables, then the problem becomes easy.  The list:  A--Z

 

You can check this in various ways (PROC CONTENTS will print the order in which the variables were created, or you can try a PROC PRINT with the VAR statement VAR A--Z).

 

At any rate, if the variables are in the proper sequence, this would work:

 

if cats(of A--Z) = repeat("N", 25) then check="N";

Super User
Posts: 10,466

Re: compact SAS code

Are there other values desired for Check? Under which conditions?

Occasional Contributor
Posts: 8

Re: compact SAS code

Thanks for the quick response. 

if any of the A-Z variables is "Y" then CHECK is "Yes", 

if all the of A-Z variables is "N" then CHECK  is "No", 

and the rest CHECK is "Unknown",  I am trying to find an easier way to code it. Any suggestions? also I realize that when I define CHECK="Yes", the length was set, so "Unknown" showed up as "Unk". Any suggestion to fix the length?

Thanks again. 

Xiaoyi

Super User
Posts: 5,071

Re: compact SAS code

I think what you are asking for is this:

 

length check $ 7;

 

Just include that in the DATA step before the IF/THEN statements that assign values to CHECK.

Super User
Super User
Posts: 6,497

Re: compact SAS code

[ Edited ]

If you don't want SAS to guess how long to make your variable then define it before you reference it.

length check $7 ;
if index(cats(of A -- Z),'Y') then check='Yes';
else if repeat('N',26-1) = cats(of A -- Z) then check='No';
else check = 'Unknown';

 

Ask a Question
Discussion stats
  • 7 replies
  • 359 views
  • 10 likes
  • 5 in conversation