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

i have a left-justified matrix of character labels that i want to right-justify for display.

 

there is an example in the SAS Help Center for the PUTC function that i have modified to teach myself:

data test;
length a b $ 6 ;

/* 123456 */
a = 'abc ' ;
b = put( a, $6. -R ) ;
put 'a=' a $6. ' b=' b $6. ;

c = right( a ) ;
put 'a=' a $6. ' c=' c $6. ;
run;

and it returns the results correctly:

a=abc b= abc
a=abc c= abc
now, i want to use PROC IML to do a similar task:
proc iml ;
a={ 'a ', 'ab ', ' abc ', 'abcd' } ;

a_put = put( a, '$6. -R' ) ; print a_put ;

a_putc = putc( a, '$6. -R' ) ; print a_putc ;

a_right =right( a ) ; print 'a=' a 'a_right=' a_right ;
quit ;
but the results are different:
rbettinger_0-1623766978912.png

note that put() is not defined:

ERROR: Invocation of unresolved module PUT.

no right-justification has been performed. where is my error? how can i achieve my goal?

 

TIA,

Ross Bettinger

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

When you use the HTML output destination, you can't use the PRINT statement (or PROC PRINT) to determine whether strings have multiple blanks because the HTML renderer "eats" extra blanks.

 

The last line of your IML program is correct. You can use the RIGHT function (and most other Base SAS functions) in SAS/IML. As you point out, PUT is on the list of exceptions.

 

The best way to verify that the spaces are really present is to translate them to some character (such as '*') that can be printed and counted. Here is how to use PROC PRINT for your example and how to use the PRINT statement in IML for your second example:

data test;
length a b $ 6 ;
/* 123456 */
a = 'abc ' ;
b = put( a, $6. -R ) ;
put 'a=' a $6. ' b=' b $6. ;
c = right( a ) ;
put 'a=' a $6. ' c=' c $6. ;
run;

/* note that the blanks do not appear when you display them in HTML */
proc print data=test;run;
/* however, you can replace blanks another character to see that they are there */
data test2;
set test;
array X[*] _CHARACTER_;
do i = 1 to dim(X);
   X[i] = translate(X[i], '*', ' ');
end;
drop i;
run;

proc iml ;
a={ 'a ', 'ab ', ' abc ', 'abcd' } ;
a_right =right( a ) ; /* right justify */

AA = a//a_right;      /* if you print AA, you won't see the blanks */
BB = translate(AA, '*', ' ');
print BB;

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

When you use the HTML output destination, you can't use the PRINT statement (or PROC PRINT) to determine whether strings have multiple blanks because the HTML renderer "eats" extra blanks.

 

The last line of your IML program is correct. You can use the RIGHT function (and most other Base SAS functions) in SAS/IML. As you point out, PUT is on the list of exceptions.

 

The best way to verify that the spaces are really present is to translate them to some character (such as '*') that can be printed and counted. Here is how to use PROC PRINT for your example and how to use the PRINT statement in IML for your second example:

data test;
length a b $ 6 ;
/* 123456 */
a = 'abc ' ;
b = put( a, $6. -R ) ;
put 'a=' a $6. ' b=' b $6. ;
c = right( a ) ;
put 'a=' a $6. ' c=' c $6. ;
run;

/* note that the blanks do not appear when you display them in HTML */
proc print data=test;run;
/* however, you can replace blanks another character to see that they are there */
data test2;
set test;
array X[*] _CHARACTER_;
do i = 1 to dim(X);
   X[i] = translate(X[i], '*', ' ');
end;
drop i;
run;

proc iml ;
a={ 'a ', 'ab ', ' abc ', 'abcd' } ;
a_right =right( a ) ; /* right justify */

AA = a//a_right;      /* if you print AA, you won't see the blanks */
BB = translate(AA, '*', ' ');
print BB;

rbettinger
Pyrite | Level 9

Thank you for helping me to understand the HTML renderer and for an excellent example demonstrating proper programming technique.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 831 views
  • 1 like
  • 2 in conversation