Help using Base SAS procedures

Right Function

Accepted Solution Solved
Reply
Contributor L_L
Contributor
Posts: 56
Accepted Solution

Right Function

Dear all

I 'would like to right allign a character variable. I'm using Right function but maybe wrongly.

Ex

data mydata;

     set mydata;

     length test $10.;

     test='score';

     test_r=right(test);

run;

I' would like to obtain

test=score

test_r=     score

What I have is test=test_r=score.

What's missing?

thank in advance


Accepted Solutions
Solution
‎08-28-2011 11:33 AM
PROC Star
Posts: 7,492

Right Function

As I mentioned, your code DID result in a variable with 5 blanks followed by the string "score".  You can test that with the following code:

data mydata;

     set sashelp.class;

     length test $10.;

     test='score';

     test_r=right(test);

     if not index(substr(test_r,1,5),"s") then

      not_padded="Yes";

     if index(substr(test_r,1,6),"s") then

      is_padded="Yes";

run;

However, unless formatted, SAS procedures will typically strip the leading blanks.  If you want to show them in, say, proc report, use a format.  For example:

proc print data=mydata;

  format test_r $char10.;

run;

View solution in original post


All Replies
PROC Star
Posts: 7,492

Right Function

Your code does result in values for test_r that have a length of 10, i.e., 5 spaces followed by the string "score".

What is it that you are trying to use it for?  You might want to read the discussion at the following thread: http://www.mathkb.com/Uwe/Forum.aspx/sas/13346/how-to-right-align-character-data-in-the-dataset

Contributor L_L
Contributor
Posts: 56

Right Function

Thanks for your help.

Yes I'm trying to obtain a variable with 5 blanks followed by the string "score". I read the discussion you proposed: the only way to right allign with blanks is using the formats?

thanks in advance

Solution
‎08-28-2011 11:33 AM
PROC Star
Posts: 7,492

Right Function

As I mentioned, your code DID result in a variable with 5 blanks followed by the string "score".  You can test that with the following code:

data mydata;

     set sashelp.class;

     length test $10.;

     test='score';

     test_r=right(test);

     if not index(substr(test_r,1,5),"s") then

      not_padded="Yes";

     if index(substr(test_r,1,6),"s") then

      is_padded="Yes";

run;

However, unless formatted, SAS procedures will typically strip the leading blanks.  If you want to show them in, say, proc report, use a format.  For example:

proc print data=mydata;

  format test_r $char10.;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 199 views
  • 3 likes
  • 2 in conversation