Not able to convert numeric variable to character

Accepted Solution Solved
Reply
Contributor VRD
Contributor
Posts: 33
Accepted Solution

Not able to convert numeric variable to character

     I have a dataset where i have few numeric variables and want to convert them into character. I used put function but it is not getting converted to character.

the variable has   TYPE: NUM  LENGTH: 8  FORMAT: BEST12.  INFORMAT: 12.


Accepted Solutions
Solution
‎05-28-2014 05:18 AM
Super User
Super User
Posts: 7,392

Re: Not able to convert numeric variable to character

Please post example data of what you are using as : strip(put(num,best.))

Should work.

View solution in original post


All Replies
Super Contributor
Posts: 336

Re: Not able to convert numeric variable to character

Number_Character=Compress(""!!Number);

like in:

Data Ex;

  Input Number;

  Number_Character=Compress(""!!Number);

  Datalines;

  2

  3

  4

  ;

Run;

.. a bit rude force, but works ..

Contributor VRD
Contributor
Posts: 33

Re: Not able to convert numeric variable to character

It didnt work. i get same result and in numeric.

Solution
‎05-28-2014 05:18 AM
Super User
Super User
Posts: 7,392

Re: Not able to convert numeric variable to character

Please post example data of what you are using as : strip(put(num,best.))

Should work.

Super Contributor
Posts: 336

Re: Not able to convert numeric variable to character

**If you copy-paste the entire code, hopefully (at least) one of the 3 ways to convert a number to a character variable should work;

Data Sample_Data;
  Input Number Best12.;
  Datalines;
  2
  3
  .
  4
  .
  82
  ;
Run;

Data S2;
  Set Sample_Data;
  Number_Character=Compress(""!!Number);
  Number_Character2=Strip(Put(Number,Best12.));
  Number_Character3=CatX('',Number);
Run;

Proc Print Data=S2 NoObs; Run;

Super User
Super User
Posts: 7,392

Re: Not able to convert numeric variable to character

Hi user24feb,

Whilst the compress and catx might work in certain circumstances, I wouldn't think that is best practice.  What you are relying on there is SAS implicitly converting character strings into numeric.  If you want to apply any kind of length/formatting etc. it wouldn't work.  The two SAS functions put and input are specifically designed to convert num->char and back again.  In my example above: strip(put(num,best.)), I have just given best. as I do not know the data in question.  However if you are programming it then you should know up front what you want the data to look like.  So if I am developing a table with 1 dp percentages I want the output to look like:

54 ( 34.5)

23 (100.0)

...

SAS would not implicitly convert as I require (i.e. 3 space before dot), I would need to use: result||" ("||put(pcent,5.2)||")".

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 730 views
  • 0 likes
  • 3 in conversation