How does the REPEAT FUNCTION deal with the trailing blanks from the string?

56 Views Asked by At

I don't understand why the second and the third observation in C2 only have one blank between the copied string and the repeated string (Shown as 'XXX XXX' and 'Y Y') while there are 5 trailing blanks in 'XXX' and 7 trailing blanks in 'Y'? Does it conflict the results in LC2?

DATA EXAMPLE1;    
     INPUT GROUP @10 STRING $3.;    
     LEFT = 'X    ';    
     RIGHT = '    X';    
     C1 = SUBSTR(GROUP,1,2) ;     
     C2 = REPEAT(GROUP,1);       
     LGROUP = LENGTH(GROUP) ;    
     LSTRING = LENGTH(STRING) ;    
     LLEFT = LENGTH(LEFT);    
     LRIGHT = LENGTH(RIGHT);    
     LC1 = LENGTH(C1);    
     LC2 = LENGTH(C2); 
     DATALINES; 
     ABCDEFGH 123 
     XXX  4 
     Y 5 ;

Here is the output: PROC PRINT OUTPUT

1

There are 1 best solutions below

2
Tom On

You posted a PHOTOGRPAH of ODS output. You cannot tell anything about spaces from either of those. Turn on the plain old LISTING output and look at that.

So assuming you ran something like this:

data example;
  LENGTH GROUP $8;
  INPUT GROUP @10 STRING $3.;
  LEFT = 'X    ';
  RIGHT = '    X';
  C1 = SUBSTR(GROUP,1,2) ;
  C2 = REPEAT(GROUP,1);
  LGROUP = LENGTH(GROUP) ;
  LSTRING = LENGTH(STRING) ;
  LLEFT = LENGTH(LEFT);
  LRIGHT = LENGTH(RIGHT);
  LC1 = LENGTH(C1);
  LC2 = LENGTH(C2);
DATALINES;
ABCDEFGH 123
XXX  4
Y 5
;

The output should look like this:

Obs    GROUP       STRING    LEFT    RIGHT    C1    C2                  LGROUP    LSTRING    LLEFT    LRIGHT    LC1    LC2

 1     ABCDEFGH     123       X        X      AB    ABCDEFGHABCDEFGH       8         3         1         5       2      16
 2     XXX                    X        X      XX    XXX     XXX            3         1         1         5       2      11
 3     Y                      X        X      Y     Y       Y              1         1         1         5       1       9

But ODS mangles the outputs to make it look "pretty".

What do you see in the SAS log if you add

put group $quote.;