Help using Base SAS procedures

converting num to char

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 444
Accepted Solution

converting num to char

tryiing to conver  a num var (id) to char by writing this step:

data new;

set old (rename=(id=id1));

id=put(id1,2.);

drop id1;

run;

and  i am getting asterix in the column of id. Does anyone know  why?


Accepted Solutions
Solution
‎06-17-2015 04:08 PM
Super User
Posts: 5,518

Re: converting num to char

You will get an asterisk whenever the numeric value is 100 or more ... so that it won't fit in a 2. format.  You could guess at the right width to use:

id = put(id1, 4.);

Or, you could run a PROC MEANS and see what the maximum value is.

Good luck.

View solution in original post


All Replies
Frequent Contributor
Posts: 130

Re: converting num to char

Looks like you're using a numeric format in your put statement.  Also, when converting a numeric variable to character, often times it gives you leading blanks.  Try modifying your code to this:

data new;

set old (rename=(id=id1));

id=strip(put(id1,$2.);

drop id1;

run;

Hope that helps!

Solution
‎06-17-2015 04:08 PM
Super User
Posts: 5,518

Re: converting num to char

You will get an asterisk whenever the numeric value is 100 or more ... so that it won't fit in a 2. format.  You could guess at the right width to use:

id = put(id1, 4.);

Or, you could run a PROC MEANS and see what the maximum value is.

Good luck.

Super Contributor
Super Contributor
Posts: 444

Re: converting num to char

Posted in reply to Astounding

right!

i thought the numbers were two digit numbers and was expecting so  but  it turns out they were all between 100 and  500 so my formatting was not correct .

Thanks Smiley Happy

Thank you all guys for the  quick  reponse

Valued Guide
Posts: 860

Re: converting num to char

Here you go:

data have;

infile cards dsd;

input num;

cards;

1

;

run;

data want(rename=(num2=num));

set have;

num2 = put(num,8.);

drop num;

run;

Occasional Contributor
Posts: 7

Re: converting num to char

The last time I did this, I didn't create a new table I just kept the old, so I wrote:

data oldtable;

set oldtable;

newvar=put(oldvar, 2.);

drop oldvar;

run;

(Actually I dropped the old variable in a different step because I needed it in another join. 

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 418 views
  • 6 likes
  • 5 in conversation