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
- /
- Data Management
- /
- Forum
- /
- Array for if then statement for large number of va...

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-03-2017 01:23 PM - last edited on 02-03-2017 03:46 PM by Reeza

I have a large number of variables for which I'd like to apply the same If then criteria:

```
if A1 = 4 or A1 = 5 then
A1_pos = 1;
if A1 = 3 or A1 = 2 or A1 = 1 then
A1_pos = 0;
if A1 = . then
A1_den = 0;
else A1_den = 1;
if A3 = 4 or A3 = 5 then
A3_pos = 1;
if A3 = 3 or A3 = 2 or A3 = 1 then
A3_pos = 0;
if A3 = . then
A3_den = 0;
else A3_den = 1;
```

Rather than write out a large number of if then statements, I'm thinking an array would could down on my code and be a more efficient way to do this. Any suggestions are greatly appreciated!

Thank you

Accepted Solutions

Solution

02-06-2017
12:18 AM

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

Posted in reply to simkinm2

02-03-2017 07:50 PM - edited 02-03-2017 07:51 PM

That is exactly what arrays are designed for. From your code it looks like you have one input array and two output arrays. Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.

```
array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
```

Now just wrap your logic inside of a DO loop and replace your variable references with array references.

```
do i=1 to dim(VAL);
if val(i) in (4,5) then pos(i)=1;
else if val(i) in (1,2,3) then pos(i)=0;
if val(i)=. then den(i)=0
else den(i)=1;
end;
```

All Replies

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

Posted in reply to simkinm2

02-03-2017 02:04 PM

Something like this should get you started. Change the 10 the largest number of like variables. Assumes that you do NOT have a variable named "a" in the data. If you do you will need a different name for the first array. Also assumes that the A variables are consecutively numbered, if that is not the case then you need to provide a list that does match. Note that you can mix sequences and single values such as Array a a1-a10 a15 a20-a26;

data want; set have; array a a1-a10; /* assumes the Pos and Den variables do not already exist. NOTE that your SAS code will run much smoother if you use the suffix as the number and not in the middle if the variables already exist you will need to list them out or possibly A1_Pos -- A10_Pos will work if they are in order in the data set (yes that is two dashes)*/ array Pos_A {10}; array Den_a {10}; do i=1 to dim(a); if a[i] in (4,5) then Pos_A[i]=1; else if a[i] in (3,2,1) then Pos_A[i]=0; Den_A[i]= not missing(A[i]); end; run;

Solution

02-06-2017
12:18 AM

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

Posted in reply to simkinm2

02-03-2017 07:50 PM - edited 02-03-2017 07:51 PM

That is exactly what arrays are designed for. From your code it looks like you have one input array and two output arrays. Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.

```
array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
```

Now just wrap your logic inside of a DO loop and replace your variable references with array references.

```
do i=1 to dim(VAL);
if val(i) in (4,5) then pos(i)=1;
else if val(i) in (1,2,3) then pos(i)=0;
if val(i)=. then den(i)=0
else den(i)=1;
end;
```