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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

3 REPLIES 3
art297
Opal | Level 21

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

L_L
Calcite | Level 5 L_L
Calcite | Level 5

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

art297
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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