## The most efficient way to carry out several statements at once

Solved
Frequent Contributor
Posts: 130

# The most efficient way to carry out several statements at once

I am relatively new to SAS, although I am learning. My only prior programming experience was with Tradestation Easy Language which, is, way more intuitive than SAS. Here, I am looking to run several statements at once. In Tradestation, it would run something like this:

If Condition 1 then begin ;

Variable1 = X

Variable2 = Y

Variable3 = Z ;

...Variable1000 = ZZ ;   (ETC.)

End ;

Ok... now in SAS, I want to do something similar. I have looked at arrays, and maybe that is the best way here, or not.

Below is the code. Now, if I had 100 different arguments or variables etc, which would be calculated if condition 1 were true, what is the most efficient way to write this code? Here, the condition is Nmiss(of Score1-Score5) eq 0. How can I only write this condition once and have the Maxscore, SecondLargest, and Minscore calculated?

```Data review.Evaluate ;
Set learn.psych ;
If Nmiss(of Score1-Score5) eq 0 then Maxscore = Max(of Score1-score5) ;
If Nmiss(of Score1-Score5) eq 0 then SecondLargest = Largest(2, of Score1-score5) ;
If Nmiss(of Score1-Score5) eq 0 then Minscore = Min(of Score1-Score5) ;
run ; ```

Accepted Solutions
Solution
‎04-26-2018 03:01 PM
Super Contributor
Posts: 269

## Re: The most efficient way to carry out several statements at once

http://documentation.sas.com/?docsetId=basess&docsetTarget=p0pcj5ajwyngron1wlsq0tet0hce.htm&docsetVe...

```   /* a more efficient method */
data updatedattractions2;
set mylib.attractions;
do;
Museums = 3;
Other = 2;
end;
```

All Replies
Solution
‎04-26-2018 03:01 PM
Super Contributor
Posts: 269

## Re: The most efficient way to carry out several statements at once

http://documentation.sas.com/?docsetId=basess&docsetTarget=p0pcj5ajwyngron1wlsq0tet0hce.htm&docsetVe...

```   /* a more efficient method */
data updatedattractions2;
set mylib.attractions;
do;
Museums = 3;
Other = 2;
end;
```
Frequent Contributor
Posts: 130

## Re: The most efficient way to carry out several statements at once

Ok. So loop, without arrays, seems to be the most efficient in this case. Thanks.

Super User
Posts: 13,942

## Re: The most efficient way to carry out several statements at once

Mostly to have an answer for others in the future with a similar question an example based directly on the question code:

```Data review.Evaluate ;
Set learn.psych ;
If Nmiss(of Score1-Score5) eq 0 then do;
Maxscore = Max(of Score1-score5) ;
SecondLargest = Largest(2, of Score1-score5) ;
Minscore = Min(of Score1-Score5) ;
end;
run ; ```

Which is an example of "factoring out the common code", the Nmiss condition.

If you have different blocks of code based on how many were missing you could use the Select block such as:

```Data review.Evaluate ;
Set learn.psych ;
select (Nmiss(of Score1-Score5));
when (0) do; <some block of code>; end;
when (1,2,3) do; <some other block of code>; end;
when (4,5) do; <some different block of code>; end;
otherwise do; <something or nothing>;end;
end;```

Which evaluates the Nmiss(of Score1-Score5) and branches to the appropriate number value indicated in the WHEN clauses within the ().

☑ This topic is solved.