Hello,
I am writing this code to create a new variable xyz with indentation. But indentation does not work when I print the data or make a report - what seems to be the problem? But if I see the dataset finalfinal1 in SAS view - indentation is there.
data finalfinal1;
set finalfinal;
length xyz aedecod aesoc $200.;
if aesoc=aedecod then xyz=aedecod;
else xyz=" "||aedecod;
proc print data=finalfinal1;
var aesoc aedecod xyz ae2 subj2 ae3 subj3 ae4 subj4 ae5 subj5;
run;
SAS studio on which operating system? Note that I did say "in Windows".
You can build a similar string using hex characters, possibly
y= repeat('A0'x,4)||x;
the Repeat function will make a string of the first character repeated the number of times in the second parameter position. So that should place 4 null characters before x.
By default Proc Print will left just justify all character output and remove leading spaces.
For some destinations, not all you would could use the STYLE override for the variable such as
var xyz / style(data)=[ASIS=on];
But that doesn't work in every ODS destination.
You can also use a different character that doesn't get treated the same as a space such as the ASCII Null character which in Windows is created by holding down the ALT key and then pressing 255 on the numeric key pad.
data example; input x $; length y $ 10; if x='abc' then y=x; else y=' '||x; /*These are ASCII null not spaces*/ datalines; abc pdq ; proc print data=example ; var x y; run;
SAS studio on which operating system? Note that I did say "in Windows".
You can build a similar string using hex characters, possibly
y= repeat('A0'x,4)||x;
the Repeat function will make a string of the first character repeated the number of times in the second parameter position. So that should place 4 null characters before x.
That is one of the "features" of using ODS output instead of plain old text. It is not PROC PRINT so much that is removing the leading spaces, but the destination formatting styles.
Did you try PROC REPORT? You should be able to right align columns in PROC REPORT using the DEFINE statement options.
Hi:
Let me outline the issues with this and suggest some solutions.
So, let me address your various points:
The LISTING window or LISTING destination or OUTPUT window is the original output location for procedure results. The destination is called a "monospace" destination because the letter i or the letter l take up the same amount of space on the line as the letter v or the letter w. So each character that you have in a variable value can be displayed in the LISTING results, as shown above. The LISTING destination or OUTPUT window will "respect" those leading spaces that you've pumped into the beginning of the variable value. In the PROC PRINT sent to the LISTING destination, the XYZ variable clearly looks indented only because the LISTING window works that way.
And, I can look at the data in a table viewer and see that xyz has been padded with leading spaces, as shown below:
But, even though I can see the leading spaces in the table viewer and the LISTING output, that does not ensure the output will be the same in other ODS destinations. For example, consider this output from the SAME program:
HTML
RTF
As you can see, the leading spaces do not get used at all when the PROC PRINT output is rendered by a browser (HTML), by WORD (RTF) or by Adobe Acrobat (PDF).
2. If your ultimate goal is RTF output -- this looks like an Adverse Event report. Most of my students from Pharmas generate RTF or PDF output for these reports -- then I would recommend using a style override for changing LEFTMARGIN using PROC REPORT in a COMPUTE BLOCK. The code for both #1 (PROC PRINT example) and #2 PROC REPORT example is pasted below:
options linesize=100 nocenter;
data final;
length aesoc $20 aedecod $35 xyz $100;
infile datalines dlm=',' dsd;
input aesoc $ aedecod $;
if aesoc=aedecod then xyz=aedecod;
else xyz=" "||aedecod;
datalines;
investigations,"investigations"
investigations,"Alanine aminotransferase"
;
run;
title; footnote;
ods listing;
ods rtf file="c:\temp\test_padding.rtf";
ods pdf file="c:\temp\test_padding.pdf";
ods html path="c:\temp" file="test_padding.html";
proc print data=final;
title '1) PROC PRINT with padding only works in LISTING destination';
var aesoc aedecod xyz;
run;
ods html close;
ods rtf close;
ods pdf close;
options center;
title; footnote;
ods rtf file="c:\temp\alt_style.rtf";
ods pdf file="c:\temp\alt_style.pdf";
ods html path="c:\temp" file="alt_style.html";
proc report data=final;
title '2) PROC REPORT with leftmargin works in RTF and PDF';
title2 'padding still works in LISTING. Neither approach works in HTML';
column aesoc aedecod xyz;
define aesoc / display;
define aedecod / display;
define xyz / display;
compute xyz;
if aesoc ne aedecod then do;
call define(_col_,'style','style={leftmargin=.25in}');
end;
else if aesoc eq aedecod then do;
call define(_col_,'style','style={font_weight=bold}');
end;
endcomp;
run;
ods html close;
ods rtf close;
ods pdf close;
title; footnote;
In order to run my program on the server, you'll have to change the locations in the FILE= and PATH= options. My guess is your server might not be a Windows server, so you'll have to find a server location where you have write access to create the files. The #1 program uses ODS LISTING and on Windows, this code populates the LISTING window for me. With SAS Studio on a server, you might have to use the FILE= option with #1 in order to write the results to an ASCII text file. However, since the leading spaces don't really work, if you're interested in producing RTF reports that you can open in Word or PDF reports that you can open in Adobe Acrobat, then I recommend just using the #2 example code and moving forward from there.
Hope this helps explain why it doesn't work and how you can achieve the indenting you want.
Cynthia
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.