BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SharifUddin
Calcite | Level 5

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;

SharifUddin_0-1648150351967.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

6 REPLIES 6
ballardw
Super User

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;
SharifUddin
Calcite | Level 5
Thanks. I tried ALT+255 - it does weird thing - delete the line where I try to put blank space using this.

By the way I am using SAS Studio.

This is really driving me crazy!
ballardw
Super User

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.

SharifUddin
Calcite | Level 5
Thanks again. My machine operates in window but I am using server version from SAS. I do not know the technical details.

Let me try with your new suggestion.

Thank you again.

Sharif
Tom
Super User Tom
Super User

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.

Cynthia_sas
SAS Super FREQ

Hi:

  Let me outline the issues with this and suggest some solutions.

  So, let me address your various points:

  1. Using your program sample to make some fake data, if you look at the data in a table viewer, you will see the leading spaces that you've added to the XYZ variable. However the ONLY destination that will show those leading spaces in the XYZ variable is the LISTING destination or the OUTPUT window as shown below in this example of PROC PRINT.

Cynthia_sas_0-1648217572060.png

 

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:

Cynthia_sas_1-1648217572061.png

 

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

Cynthia_sas_2-1648217572062.png

 

RTF

Cynthia_sas_3-1648217572064.png

 

PDF

Cynthia_sas_4-1648217572067.png

 

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

SAS Innovate 2025: Register Now

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!

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
  • 6 replies
  • 1482 views
  • 3 likes
  • 4 in conversation