turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Array Puzzle

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-09-2016 03:20 PM

I have two variables, X and Y, plus another variable S. X and Y are integers in the range 1-7. S is a character variable that takes the value '1' and some others. I want to create a series of flags, A1-A4, according to these rules.

- IF S='1', set the flag corresponding to the larger of X and Y, subject to the maximum of 4. For example, if X=1 and Y=2, then set A2 = 1, leaving the other flags equal to zero. If X=2 and Y=6, set A4=1, etc.
- IF S is anything else, set the flag corresponding to Y, with the same maximum (ignore X entirely).

I first wrote this:

```
DATA Test;
SET Source;
ARRAY A[4] A1-A4 (0 0 0 0);
IF S = '1' THEN
A[MIN(4,MAX(X,Y))] = 1;
ELSE
A[MIN(4,Y)] = 1;
RUN;
```

As I read that, it should change only one of the A1-A4 flags for each record. But what I find is that ALL of the values of A1-A4 get set to 1.

If I do this,

```
DATA Test;
SET Source;
A1 = 0;
A2 = 0;
A3 = 0;
A4 = 0;
ARRAY A[4] A1-A4;
IF S = '1' THEN
A[MIN(4,MAX(X,Y))] = 1;
ELSE
A[MIN(4,Y)] = 1;
RUN;
```

I get what I want. What is the difference?

Accepted Solutions

Solution

06-10-2016
07:57 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-09-2016 03:27 PM

The inital values for the array implies RETAIN of those variables.

All Replies

Solution

06-10-2016
07:57 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-09-2016 03:27 PM

The inital values for the array implies RETAIN of those variables.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-10-2016 08:03 AM

Thanks to all who replied. I seldom use the ability to intialize arrays, and I see that there were implications of doing that which I didn't realize.

Thanks also to those who showed me how to make the code more compact. Those are clever uses of SAS functions. I'm not sure if I will use them. I fear that a few years from now, someone (possibly me) will look at the code and say, "What the heck does *that* do?" I don't want my code to be more clever than my ability to understand it!

--Dav

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-09-2016 07:32 PM

ARRAY A[4] A1-A4 (0 0 0 0);

initialises the values only once: when the array is created.

There are 2 cases when array values are not reset in the data step loop: when they are temporary, and when their values are initialised.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-10-2016 05:14 AM

If it helps, you can shrink your if clauses:

data source; s="1"; x=3; y=3; output; s="2"; x=1; y=6; output; run; data test; set source; array a[4] (0 0 0 0); a[min(4,max(x,y))]=ifn(s='1',1,0); run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-10-2016 05:33 AM

Or a[min(4,max(x,y))]=(s='1');

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-10-2016 06:06 AM

Side note: The specifiction of an initial value for *one* array variable implies RETAIN for *all* variables of the array, including those which are not initialized.