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.
I think you just need to add a $, i.e. length=$5
I think you just need to add a $, i.e. length=$5
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;
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.
yes sir, that is correct.
Thanks for your comment!
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
This will work without having to read the data twice.
data check2;
set check1(rename=(b=tempb));
b=put(tempb,5.);
drop tempb;
run;
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;
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.
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.
sorry, i missed that the variable needed to be he first inorder. You are correct.
thanks all for your comment...
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.
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.