Help using Base SAS procedures

How to convert numeric values to characters without adding blanks

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

How to convert numeric values to characters without adding blanks

Hello Smiley Happy

I have a numeric variable x with values ranging from 1, 2 to 25. If I simply use: y = PUT(x,  2.) then there will be one blank space for those x with one single digit, because the PUT function makes the length of 2 for all y.

My question is: how can I make this conversion without adding any blanks to y?


Accepted Solutions
Solution
‎11-07-2011 09:00 AM
PROC Star
Posts: 7,483

How to convert numeric values to characters without adding blanks

If you were outputting the data to a text file, I'd suggest using the -l modifier.  However, if you simply want to remove the extra spaces in the data itself, you could always just "strip" them.  e.g.,

data test;

  set sashelp.class;

  if _n_ lt 6 then age=_n_;

  age1char=put(age,2.);

  age2char=strip(put(age,2.));

  x=length(age1char);

  y=length(age2char);

run;

View solution in original post


All Replies
Solution
‎11-07-2011 09:00 AM
PROC Star
Posts: 7,483

How to convert numeric values to characters without adding blanks

If you were outputting the data to a text file, I'd suggest using the -l modifier.  However, if you simply want to remove the extra spaces in the data itself, you could always just "strip" them.  e.g.,

data test;

  set sashelp.class;

  if _n_ lt 6 then age=_n_;

  age1char=put(age,2.);

  age2char=strip(put(age,2.));

  x=length(age1char);

  y=length(age2char);

run;

Respected Advisor
Posts: 3,799

How to convert numeric values to characters without adding blanks

You could also use -L with the PUT function.  PUT(AGE,2.-L)

PROC Star
Posts: 7,483

How to convert numeric values to characters without adding blanks

Posted in reply to data_null__

DN, Totally agree!  But, I think it is really just a matter of preference as there is virtually NO performance difference between the two methods (at least when tested on 1.9 million records).

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 116 views
  • 0 likes
  • 3 in conversation