Desktop productivity for business analysts and programmers

converting numeric value to character error

Accepted Solution Solved
Reply
Regular Contributor
Posts: 220
Accepted Solution

converting numeric value to character error

Hi guys, need help plz.

I convert variable 'b' from numeric to character by applying following code and then drop 'b' and then rename a=b.

then try to bring b as first variable in dataset by using 'attrib' funtion but getting following error msg.

can you plz help?

data check2;

set check1;

a=put(b,5.);

drop b;

rename a=b;

run;

data check3;

attrib b length=5;

set check2;

run;

ERROR: variable B has been defined as both character and numeric.



Accepted Solutions
Solution
‎03-28-2013 10:48 AM
Esteemed Advisor
Posts: 7,290

Re: converting numeric value to character error

I think you just need to add a $, i.e. length=$5

View solution in original post


All Replies
Solution
‎03-28-2013 10:48 AM
Esteemed Advisor
Posts: 7,290

Re: converting numeric value to character error

I think you just need to add a $, i.e. length=$5

Regular Contributor
Posts: 220

Re: converting numeric value to character error


hello sir, I did try this and it worked. I didn't drop and rename, used 'retain' function and didn't use 'length' statement ...

data check2;

set check1;

a=put(b,5.);

run;

data check3;

retain b;

set check2;

run;

Esteemed Advisor
Posts: 7,290

Re: converting numeric value to character error

Either would have worked.  However, I hope you just forgot to post the rename statement as, otherwise, the code you showed would create the new character variable a, but then put the old numeric variable b in the first position.

Regular Contributor
Posts: 220

Re: converting numeric value to character error

yes sir, that is correct.

Thanks for your comment!

SAS Employee
Posts: 26

Re: converting numeric value to character error

Here are some samples from support.sas.com that may be useful:

Sample 40700: How to convert all character variables to numeric and use the same variable names in the output data set

http://support.sas.com/kb/40700

Sample 24590: Convert values from character to numeric or from numeric to character

http://support.sas.com/kb/24590

Sample 43052: Convert variables containing only numeric values from type character to numeric

http://support.sas.com/kb/43052

SAS Employee
Posts: 3

Re: converting numeric value to character error

This will work without having to read the data twice.

data check2;

   set check1(rename=(b=tempb));

   b=put(tempb,5.);

   drop tempb;

run;

  

Esteemed Advisor
Posts: 7,290

Re: converting numeric value to character error

Larry,

It would need one additional statement in order to satisfy the OP's needs:

data check2;

  attrib b length=$5;

  set check1(rename=(b=tempb));

  b=put(tempb,5.);

  drop tempb;

run;

SAS Employee
Posts: 3

Re: converting numeric value to character error

The put function would create a character variable of length 5 because of the $5. format in the put function. the attrib is not needed.

Esteemed Advisor
Posts: 7,290

Re: converting numeric value to character error

The attrib statement (or any statement that would reorder variables) is needed because the OP wanted variable b to be placed in the first position.

SAS Employee
Posts: 3

Re: converting numeric value to character error

sorry, i missed that the variable needed to be he first inorder. You are correct.

Regular Contributor
Posts: 220

Re: converting numeric value to character error

thanks all for your comment...

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 460 views
  • 0 likes
  • 4 in conversation