BookmarkSubscribeRSS Feed
Xiaoyi
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? 

thanks. 

 

Xiaoyi

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

  end;

run;

Tom
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.

Astounding
PROC Star

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

ballardw
Super User

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

Xiaoyi
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. 

Xiaoyi

Astounding
PROC Star

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.

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

 

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!

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.

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
  • 7 replies
  • 1542 views
  • 10 likes
  • 5 in conversation