Hi friends,
I have the problem.
and
I have the sas data set like this
emp_details value
sal 5,25,369
HRA 10.2%
PF 5%
TAX 1%
LIC 10%
OTHER_INCOME 60,500
OTHER_SAVES .
TA 0
HERE emp_details AND value BOTH ARE CHARACTER DATA TYPE .,
I WANT CONVERTING value VARIABLE CHARACTER TO NUMERIC.
AND ALSO GET THE SAME FORMATS
emp_details value
sal 5,25,369
HRA 10.2%
PF 5%
TAX 1%
LIC 10%
OTHER_INCOME 60,500
OTHER_SAVES .
TA 0
HERE VALUE IS THE NUMERIC DATA TYPE
PLZ HELP ME
data have;
input emp_details : $20. value : $20.;
cards;
sal 5,25,369
HRA 10.2%
PF 5%
TAX 1%
LIC 10%
OTHER_INCOME 60,500
OTHER_SAVES .
TA 0
;
run;
proc format;
value fmt
low-0,1-high=[comma32.]
other=[percent8.]
;
run;
data want;
set have;
length fmt $ 20;
if find(value,'%') then fmt='percent12.';
else fmt='comma32.';
v=inputn(strip(value),fmt);
format v fmt.;
run;
Please don't type in capitals. You can't directly convert each of those text items to numeric as they do not contain the same type of data, hence why they are character in the first place. A format is something which applies to the whole of a column, so to apply percentage will set that format on every cell in that column. SAS is not Excel. SAS works on the concept of datasets with fixed structure columns.
Now the second question is why you need to put them into numeric? Is it to do further processing on the data? If so then create separate columns for each type of data, i.e. one for percentages, one for numbers. Then process then and return the processed information back to character to create one column. E.g
emp_details value num_val perc_val
HRA 5,25,369 525369
PF 5% 5
...
Then cats(put(num_val,comma8.),put(perc_val,percent8.)).
data have;
input emp_details : $20. value : $20.;
cards;
sal 5,25,369
HRA 10.2%
PF 5%
TAX 1%
LIC 10%
OTHER_INCOME 60,500
OTHER_SAVES .
TA 0
;
run;
proc format;
value fmt
low-0,1-high=[comma32.]
other=[percent8.]
;
run;
data want;
set have;
length fmt $ 20;
if find(value,'%') then fmt='percent12.';
else fmt='comma32.';
v=inputn(strip(value),fmt);
format v fmt.;
run;
code is working sir.
thank you
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.