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.
Please post example data of what you are using as : strip(put(num,best.))
Should work.
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 ..
It didnt work. i get same result and in numeric.
Please post example data of what you are using as : strip(put(num,best.))
Should work.
**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;
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)||")".
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.