BookmarkSubscribeRSS Feed
JAR
Obsidian | Level 7 JAR
Obsidian | Level 7
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;

Thanking you in advance,
Jijil Ramakrishnan
4 REPLIES 4
ChrisNZ
Tourmaline | Level 20
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

Suggested Google advanced search arguments, this topic / post:
code performance considerations site:sas.com

debugging techniques site:sas.com
chang_y_chung_hotmail_com
Obsidian | Level 7
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        .         .         .


   */

JAR
Obsidian | Level 7 JAR
Obsidian | Level 7
Thankyou Chang. This is helpful.

Regads,
Jijil

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1192 views
  • 0 likes
  • 4 in conversation