Hello everyone,
I have a simple question. I want to add some blank space to the front of a variable. I tries '||', but it seems not work. For example,
data a;
input color $;
datalines;
Red
Blue
Yellow
White
;
But I want values of color like this : " Red", " Blue", " Yellow", " White". There are some blanks in front of each. Any suggestions? Thanks.
The concatenation operator is a good way to add spaces so I suspect it actually worked. But getting the spaces to appear in the output will depend on how you are generating the output and what output destination you are using.
For example if I use your data. And just use PROC PRINT.
data have ;
length color indented $10 ;
input color ;
indented = ' ' || color;
datalines;
Red
Blue
Yellow
White
;
proc print data=have;
run;
It does not look like the spaces are there.
Obs color indented 1 Red Red 2 Blue Blue 3 Yellow Yellow 4 White White
But if you use the $CHAR format with the INDENTED variable
proc print data=have;
format indented $char10. ;
run;
then you can see them.
Obs color indented 1 Red Red 2 Blue Blue 3 Yellow Yellow 4 White White
But that doesn't help with HTML output destination. There both of the printouts look the same
Why? How you store a character variable and how you output it or print it are two separate things. You can always add blanks or change the alignment of text when you are printing.
Hi @lc7033907 You could use RIGHT function or PUT function with -R argument to right align the value, thus creating a leading blank-
data a;
input color $;
datalines;
Red
Blue
Yellow
White
;
data want;
set a;
need=right(color);
run;
/*OR*/
data want;
set a;
need=put(color,$8. -r);
run;
You alignment values right, and there are distinct space in the from. Actually I like the same indent for all those values. Let me modify my question, so you can get more information.
Here is my original dataset:
data a;
input Family $1-4 color $ 6-12 Num;
datalines;
Xu 5
Red 1
Blue 3
Chen 2
Yellow 0
White 1
;
I want my result to print like
Xu 5
Red 1
Blue 3
Chen 2
Yellow 0
White 1
There are the same indents in front of color value. Any suggestions?
There are ways to get indentation without adding leading blanks. However, they involve printing COLOR in a separate column rather than indented. The report format changes slightly, but you might even consider it an improvement. For example, skip any concatenation and try:
proc print data=have;
by family;
id family;
var color num;
run;
:Or:
proc tabulate data=have missing order=data noseps;
class family color;
var num;
tables family*color, num*sum=' ';
run;
Here is an example of using a custom Format and a report procedure to accomplish this with a single variable and not have to do a separate summary to get the counts as shown.
Proc format; value ex (multilabel notsorted) 1,2,3 = 'Group 1' 1= ' Site 1' 2= ' Site 2' 3= ' Site 3' 4,5,6 = 'Group 2' 4= ' Site 4' 5= ' Site 5' 6= ' Site 6' ; data example; do i=1 to 50; Site= rand('integer',6); output; end; run; proc tabulate data=example; class site / mlf preloadfmt order=data; classlev site /style=[Asis=yes]; format site ex.; table site, n='Count' /printmiss row=float ; run;
Multilabel formats are only used by a small number of procedures such as Proc Tabulate, Report and Summary. If the format is used with a procedure other than these the results can be interesting. The ORDER of the value the value ranges in the Value statement of Proc format is very important.
The data step is just to provide an example of data creating some values in the range of values in the format.
Proc Tabulate is used to show the results. The MLF on the Class statement says the expected format will use multilabels, Preloadfmt forces all the values in the format to appear in the specified order combined with the Order=data option (Proc Tabulate will default to formatted values to order and not what is wanted here).
The CLASSLEV provides style options for how the Class variable is presented. The ASIS=Yes means in this case to preserve the leading spaces in the formatted values.
The Table option Printmiss works with the Preloatfmt option to ensure all levels of the format are shown if any specific value does not actually appear in the data.
I present this option as you may have some difficulty later using groups to get other statistics.
We could modify the tabulate to get statistics of other variables using the same groupings:
proc tabulate data=example; class site / mlf preloadfmt order=data; classlev site /style=[Asis=yes]; format site ex.; var i ; table site, n='Count' i*(sum='Total of I' mean='Mean of I') /printmiss row=float ; run;
Thank you so much. This is what I want.
The concatenation operator is a good way to add spaces so I suspect it actually worked. But getting the spaces to appear in the output will depend on how you are generating the output and what output destination you are using.
For example if I use your data. And just use PROC PRINT.
data have ;
length color indented $10 ;
input color ;
indented = ' ' || color;
datalines;
Red
Blue
Yellow
White
;
proc print data=have;
run;
It does not look like the spaces are there.
Obs color indented 1 Red Red 2 Blue Blue 3 Yellow Yellow 4 White White
But if you use the $CHAR format with the INDENTED variable
proc print data=have;
format indented $char10. ;
run;
then you can see them.
Obs color indented 1 Red Red 2 Blue Blue 3 Yellow Yellow 4 White White
But that doesn't help with HTML output destination. There both of the printouts look the same
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.