Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- compact SAS code

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-05-2016 11:27 AM
(2013 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.

Ready to level-up your skills? Choose your own adventure.