DATA Step, Macro, Functions and more

datatype conversion

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

datatype conversion

HI,

 

I have a dataset.

 

Data a;

var1 var2 var3;

RUN;

 

var1,2,3 are numeric. How to I convert their datatype to be character?

 

Thanks,

Archana


Accepted Solutions
Solution
‎04-20-2016 03:29 PM
Trusted Advisor
Posts: 1,118

Re: datatype conversion

[ Edited ]
Posted in reply to ArchanaSudhir

Hi  Archana,

 

I'm not sure what "differences" you refer to.

 

Please note that BESTw. format always results in a string of length ("width") w (here: w=10). These w characters can include

  • leading blanks
  • a minus sign
  • digits left of the decimal point
  • a decimal point
  • digits right of the decimal point
  • exponential notation like "E-6" for 10^-6

If you want to obtain a fixed number of decimal places (which you didn't tell in your initial post), BESTw. is inappropriate. Use the w.d format instead, with d=4 and a sufficiently large w, for example w=17 (this will most likely be larger than necessary).

 

Var1_char = put(var1, 17.4);

 

View solution in original post


All Replies
Super User
Posts: 19,851

Re: datatype conversion

Posted in reply to ArchanaSudhir

Use a PUT() statement. 

Make sure to use appropriate format, best32 is an example. 

 

Also note that you cannot change a variables type so you have to create a new variable. 

 

Var1_char = put(var1, best32.);
Contributor
Posts: 53

Re: datatype conversion

Thanks.

 

But I am getting differences by using best10.

Var4 var5
0.99079152 0.999999813
   
Var4 var5
0.9908 1.0000

 

I should get the answer in 4 decimal places.

How do I get it?

 

Thanks,

Archana

Super User
Posts: 11,343

Re: datatype conversion

Posted in reply to ArchanaSudhir

ArchanaSudhir wrote:

Thanks.

 

But I am getting differences by using best10.

Var4 var5
0.99079152 0.999999813
   
Var4 var5
0.9908 1.0000

 

I should get the answer in 4 decimal places.

How do I get it?

 

Thanks,

Archana


Pick an appropriate format. If you require a specific number of decimal places then you need to provide a format that will force them. Without knowing ANY details of your data or output requirement you get a generic response. Try

Put(var,f10.4);

The number after the f has to be large enough to accomate the largest value including 4 spaces for decimal values and 1 for the decimal itself. So if you have values like 1234567.8787 you would need f12.4. But don't expect us to guess correctly without all of the details.

Solution
‎04-20-2016 03:29 PM
Trusted Advisor
Posts: 1,118

Re: datatype conversion

[ Edited ]
Posted in reply to ArchanaSudhir

Hi  Archana,

 

I'm not sure what "differences" you refer to.

 

Please note that BESTw. format always results in a string of length ("width") w (here: w=10). These w characters can include

  • leading blanks
  • a minus sign
  • digits left of the decimal point
  • a decimal point
  • digits right of the decimal point
  • exponential notation like "E-6" for 10^-6

If you want to obtain a fixed number of decimal places (which you didn't tell in your initial post), BESTw. is inappropriate. Use the w.d format instead, with d=4 and a sufficiently large w, for example w=17 (this will most likely be larger than necessary).

 

Var1_char = put(var1, 17.4);

 

Regular Contributor
Posts: 212

Re: datatype conversion

[ Edited ]
Posted in reply to ArchanaSudhir

Hi mate,

 

Just setup a new variable name and use put(var1,best10.);

 

cha1 = put(var1,best10.);
cha2 = put(var2,best10.);
cha3 = put(var3,best10.);

That's all

 

One more advice: put() is from numeric to character and input is the opposite.

 

Hope this helps

But remember the format must fit the size of your number, that's why i used the best10., if your number have more than 10 number before the comma, use a greater number format.

Super User
Posts: 11,343

Re: datatype conversion

Posted in reply to ArchanaSudhir

Are you attempting to change variables in an existing data set or create as in your example?

 

With your "example"

data a;

   length Var1 Var2 Var3 $ 10; /* specify number to be largest number of characters you'll need the variable to hold*/

run;

 

If in an existing data set, while learing SAS the best idea would be to make a new dataset, rename your old variables (data set option) and use something like Var1 = put(OldVar1,best12.); pick an appropriate format for the appearance you want.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 291 views
  • 0 likes
  • 5 in conversation