Contributor
Posts: 45

# Efficient Coding!

Hi,
I am new to SAS (programing in general). Could someone tell me how to write this code efficiently?

if(Math1=. and Math2~=.) then do;math1=math2; math2=.; end; else
if(Math1=. and Math3~=.) then do;math1=math3; math3=.; end;
if(Math2=. and Math3~=.) then do;math2=math3; math3=.; end;

Jijil Ramakrishnan
PROC Star
Posts: 2,370

## Re: Efficient Coding!

I don't see how you could implement the same logic better.

Fyi, in some cases
[pre]if missing(MATH1) then MATH1=coalesce(MATH2,MATH3);[/pre]
could be useful to you, but wouldn't help here. Note that
[pre]if(Math2=. and Math3~=.) then do;math2=math3; math3=.; end; [/pre]
can be written
[pre]if Math2=. then do;math2=math3; math3=.; end; [/pre]
with the same result. Not that it matters, just fyi again.
Super Contributor
Posts: 3,176

## Re: Efficient Coding!

Suggest some self-initiated desk-checking for SAS performance, which many times is data-dependent and application-dependent.

For example, using SUBSTR with lead-character test, such as the statement construct IF SUBSTR(,1,..) is much more expensive with iterating a million times than just using the coding technique IF =: instead. And, not so much a concern, when iterating a DATA step execution only a few times.

You can setup a "test scenario" DO / END loop, as demonstrated below to explore cause/effect with various data-scenarios and coding constructs:

OPTIONS FULLSTIMER;
* SAMPLE DATA STEP TO TEST OPER PERFORMANCE FOR SUBSTR. ;
DATA _NULL_;
RETAIN A 'XXX';
DO I=1 TO 1E6;
IF SUBSTR(A,1,1) = 'Y' THEN ;
* IF A =: 'Y' THEN ;
END;
RUN;

Also, for testing code-path for desired / expected results, suggest using the PUTLOG ">DIAG-nn>" / _ALL_; statement, when used tactically to detect SAS variable value conditions at desired points in a DATA step process/flow -- the use of "nn" helps identify each unique processing point, when there are more than one PUTLOG statements being used.

Scott Barry
SBBWorks, Inc.

code performance considerations site:sas.com

debugging techniques site:sas.com
Regular Contributor
Posts: 241

## Re: Efficient Coding!

Maybe this is what you want to do? This is *very* inefficient, but does work.

/* test data */

data one;

input score1 - score3;

cards;

11 12 13

21  . 23

. 32 33

.  . 43

51  .  .

.   . 63

.  .  .

;

run;

/* "compact" the scores by bubbling non-missings to the left */

data two;

set one;

array scores score1-score3;

drop i j temp;

do i = 1 to 3-1;

do j = 3 to i+1 by -1;

if missing(scores(j-1)) and not missing(scores(j)) then do;

temp = scores(j-1);

scores(j-1) = scores(j);

scores(j) = temp;

end;

end;

end;

run;

/* check */

proc print data=two;

run;

/* on lst

Obs    score1    score2    score3

1       11        12        13

2       21        23         .

3       32        33         .

4       43         .         .

5       51         .         .

6       63         .         .

7        .         .         .

*/

Contributor
Posts: 45

## Re: Efficient Coding!

Jijil
Discussion stats
• 4 replies
• 230 views
• 0 likes
• 4 in conversation