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
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;
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
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.