HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

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


Accepted Solutions
Solution
‎03-24-2015 08:43 AM
Grand Advisor
Posts: 9,593

Re: HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

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;

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

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

Solution
‎03-24-2015 08:43 AM
Grand Advisor
Posts: 9,593

Re: HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

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;

Contributor
Posts: 40

Re: HOW TO GET THE SAME FORMATS, CONVERTING VARIABLE CHARACTER TO NUMERIC.

code is working sir.

thank you

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 204 views
  • 0 likes
  • 3 in conversation