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

sas-innovate-2024.png

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.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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