BookmarkSubscribeRSS Feed
Obsidian | Level 7

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? 




Garnet | Level 18

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';



Super User Tom
Super User

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.

Opal | Level 21

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

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

Obsidian | Level 7

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. 


Opal | Level 21

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 Tom
Super User

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';




Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 7 replies
  • 5 in conversation