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

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.


1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

View solution in original post

11 REPLIES 11
art297
Opal | Level 21

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

sas_9
Obsidian | Level 7


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;

art297
Opal | Level 21

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.

sas_9
Obsidian | Level 7

yes sir, that is correct.

Thanks for your comment!

jwsquillace
SAS Employee

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

Larry_S_sas
SAS Employee

This will work without having to read the data twice.

data check2;

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

   b=put(tempb,5.);

   drop tempb;

run;

  

art297
Opal | Level 21

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;

Larry_S_sas
SAS Employee

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.

art297
Opal | Level 21

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.

Larry_S_sas
SAS Employee

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

sas_9
Obsidian | Level 7

thanks all for your comment...

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 11 replies
  • 3257 views
  • 0 likes
  • 4 in conversation