data have ;
input number ;
cards ;
22.34
3.4
33
323.322
;
I have above data set Now how to keep right side alignment with data type is character .
have... want
22.34 22.34
3.4 3.4
33 33
323.322 323.322
How to create want variable?
Note:Want is new character variable It should be in right side.
Use the RIGHT() function to right-align character expressions:
data have ;
input number ;
cards ;
22.34
3.4
33
323.322
;
data want;
set have;
length char $8;
char = right(put(number,best8.));
run;
proc print data=want noobs;
run;
Result (LISTING output):
number char 22.340 22.34 3.400 3.4 33.000 33 323.322 323.322
Do you want to right-align the text in the dataset or in a report using these data?
@thanikondharish wrote:
In dataset only....
Where it IS right-aligned, see my previous post.
What you see in the Table Viewer does not reflect the alignment correctly because it uses a proportional font (where the blanks are narrower than the characters).
It IS right-aligned. Just take a short look at the result I posted. Note that most output destinations apart from LISTING will automatically left-align all character values, and discard any leading blanks. But that does NOT change the value stored in the variables. If you want further proof, run this:
data want;
set have;
length char $8;
char = right(put(number,best8.));
char_hex = put(char,$hex16.);
run;
and you will see the blanks (hex 20) in the variable.
Please state exactly what you are aiming for.
I agree with @Kurt_Bremser
Don't rely on the viewer to assess the alignment (the font has not the same size for all characters : a dot is "smaller" than a digit, ..)
As an alternative to the right function, you can also use the '-r' modifier in the put statement:
char=put(number, best8. -r);
Best,
In the code example I gave you, you can see in the hex display that there are NO trailing spaces stored AT ALL.
If you run different code, show us.
THEN WHAT IS YOUR "POINT OF VIEW"?
I have sufficiently proven that the contents are right-aligned, in the data and in LISTING output.
@thanikondharish wrote:
Note tralaing.
There is no correct alignment as my point of view
@thanikondharish wrote:
Note tralaing.
There is no correct alignment as my point of view
In my occasionally humble opinion the alignment of values in a data set is completely irrelevant.
Since alignment really only affects people reading the data for most cases then when the data is written out in a report is when any alignment becomes important. And the reporting procedures have a number of methods to adjust alignment.
Of if the data is to be written to a text file read by another program then use appropriate syntax to align things at the time the text file is created.
@thanikondharish wrote:
Thanq for your reply.
However coming tralaing spaces.
[image: image.png]
If you want to insert a picture use the Insert Photos icon on the forum editor.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.