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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 3594 views
  • 0 likes
  • 3 in conversation