As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Indentation"

From sasCommunity
Jump to: navigation, search
(mv to Category:Best Practices & add wikilinks)
(See also: Code Like It Matters)
 
Line 11: Line 11:
 
%IF .. %DO block of statements, a [[PROC SQL]] step, a [[DATA step]] and a  
 
%IF .. %DO block of statements, a [[PROC SQL]] step, a [[DATA step]] and a  
 
CREATE statement.  
 
CREATE statement.  
 
  
 
  %if %length(&byvar) %then %do;  
 
  %if %length(&byvar) %then %do;  
Line 30: Line 29:
 
   run;  
 
   run;  
 
  %end;
 
  %end;
 +
 +
== See also ==
 +
* [[Code Like It Matters: Writing Code That's Readable and Shareable]]
  
 
[[Category:Best Practices]]
 
[[Category:Best Practices]]

Latest revision as of 12:00, 10 November 2017

When indenting code you should indent the termination character (or command) the same number of spaces as the original command. Unlike other languages that use {} or other methods for specifying sub-blocks of code it matters in SAS that you end the nested block with the proper termination. When the termination is at the same indentation as the initial command then when scanning the code you not only see the end of the original statement but can also confirm that it is ending with the proper termination.

This rule can be applied consistently to PROC and DATA steps, nested groups of statements or to a single statement that spans multiple lines. This snippet shows how to indent a macro %IF .. %DO block of statements, a PROC SQL step, a DATA step and a CREATE statement.

%if %length(&byvar) %then %do; 
*----------------------------------------------------------------------; 
* Make sure all treatment groups are defined for all by groups ; 
*----------------------------------------------------------------------; 
  proc sql; 
    create table n_all as 
      select *,0 as n,0 as nsubj 
      from (select distinct &byvar from subjects) 
         , (select distinct trtvar, trtlabel from subjects) 
      order by &byvar, trtvar, trtlabel 
    ; 
  quit; 
  data nsubj2; 
    merge n_all nsubj2; 
    by &byvar trtvar trtlabel; 
  run; 
%end;

See also