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 ;
/* a more efficient method */ data updatedattractions2; set mylib.attractions; if City = 'Madrid' then do; Museums = 3; Other = 2; end;
/* a more efficient method */ data updatedattractions2; set mylib.attractions; if City = 'Madrid' then do; Museums = 3; Other = 2; end;
Ok. So loop, without arrays, seems to be the most efficient in this case. Thanks.
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 ().
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.