The Case of the Missing Blanks: Why SAS Output Might Not Show Multiple Blanks in Strings
Understand why SAS output sometimes collapses multiple blanks in string data and how to control this behavior. This article covers the nuances of blank handling in different output destinations and how to preserve spacing.
The Problem
When displaying text with multiple consecutive spaces, SAS output often shows only single spaces, causing formatting issues and potential data quality concerns.
Why This Happens
HTML Output Behavior
HTML collapses multiple spaces into single spaces by default (HTML specification behavior).
ODS Destinations
Different ODS destinations handle spaces differently:
- HTML: Collapses multiple spaces
- PDF: Usually preserves spaces
- RTF: Generally preserves spaces
- LISTING: Preserves spaces
Solutions
Method 1: Using Non-Breaking Spaces
data formatted;
text = "Multiple spaces here";
/* Replace regular spaces with non-breaking spaces */
text_preserved = tranwrd(text, ' ', ' ');
run;
ods html file="output.html";
proc print data=formatted noobs;
var text_preserved;
run;
ods html close;Method 2: HTML PRE Tags
ods html file="output.html";
data _null_;
file print ods;
put '<pre>';
put "Text with multiple spaces";
put '</pre>';
run;
ods html close;Method 3: CSS Styling
ods html file="output.html"
style=styles.default
css="white-space: pre;";
proc print data=mydata;
run;
ods html close;Method 4: Using $CHAR Format
data test;
length text $50;
text = "Multiple spaces";
format text $char50.;
run;
proc print data=test noobs;
run;Preserving Alignment
Fixed-Width Font
ods html file="output.html";
proc print data=data;
style(data) = [font_face="Courier New"
white_space=pre];
run;
ods html close;Best Practices
- Choose Right Destination: Use PDF or RTF if spacing is critical
- Test Output: Always verify spacing in target format
- Document Behavior: Note spacing requirements
- Use Monospace Fonts: For alignment-dependent output
- Consider Alternatives: Use tables or formats instead of spaces
Understanding $CHAR vs $
The $CHAR format preserves trailing blanks, while $ (standard character format) trims them.
data compare;
text1 = "test "; /* With trailing spaces */
text2 = text1;
format text1 $char10. text2 $10.;
run;
proc print; run;Conclusion
Understanding how SAS and ODS destinations handle spaces is crucial for producing correctly formatted output. Choose the appropriate method based on your output destination and formatting requirements.