BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

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.

 

13 REPLIES 13
Kurt_Bremser
Super User

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
thanikondharish
Calcite | Level 5
Thanq for your reply.
However it is not working and then alignment not coming properly in dataset
ed_sas_member
Meteorite | Level 14

Hi @thanikondharish 

 

Do you want to right-align the text in the dataset or in a report using these data?

Kurt_Bremser
Super User

@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).

Kurt_Bremser
Super User

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.

ed_sas_member
Meteorite | Level 14

Hi @thanikondharish 

 

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,

thanikondharish
Calcite | Level 5
Thanq for your reply.
However coming tralaing spaces.
[image: image.png]
thanikondharish
Calcite | Level 5
Note tralaing.
There is no correct alignment as my point of view
Kurt_Bremser
Super User

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

 

ballardw
Super User

@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.

Tom
Super User Tom
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 13 replies
  • 5764 views
  • 1 like
  • 5 in conversation