BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
VRD
Calcite | Level 5 VRD
Calcite | Level 5

     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.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Should work.

View solution in original post

5 REPLIES 5
user24feb
Barite | Level 11

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

VRD
Calcite | Level 5 VRD
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Should work.

user24feb
Barite | Level 11

**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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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