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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

image.png

 

 

View solution in original post

10 REPLIES 10
SASKiwi
PROC Star

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.

lc7033907
Obsidian | Level 7
I am working on a project now. I made one column, which was created by the
other two, for example, the new column called A, the original two columns
called A1 and A2. I want to print it like the following:

When all values are from A1, JUST=L; when all values are from A2, I want to
indent them a little bit. Let me modify my problem.

data a;
input Family $ color $;
datalines;
Xu Red
Xu Blue
Chen Yellow
Chen White
;


I want my result to show like:


Xu
Red
Blue

Chen
Yellow
White

There are some indents for those colors. Any suggestions? Thanks.


novinosrin
Tourmaline | Level 20

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;

 

lc7033907
Obsidian | Level 7

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?

 

Astounding
PROC Star

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;
lc7033907
Obsidian | Level 7
I see your meaning. Thanks. I might need to reconsider this problem and see
if there is another way to solve it. Thanks.
ballardw
Super User

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;

 

lc7033907
Obsidian | Level 7
I see. Thank you so much. Now I am much clearer. Really appreciate it. 😁
lc7033907
Obsidian | Level 7

Thank you so much. This is what I want.

Tom
Super User Tom
Super User

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

image.png

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 10 replies
  • 9834 views
  • 2 likes
  • 6 in conversation