SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Converting the numeric part of a numeric variable into its fraction

Reply
Contributor
Posts: 24

Converting the numeric part of a numeric variable into its fraction

HI,

 

I have a dataset of 14 columns.

One of those variables show the percentage of students. e.g 25%

I need to remove the % and replace it with it's corresponding fraction. e.g 25/100 = .25

 

Please let me know if I can do this together.

 

However, what I have doen is just stripped the % from the column. How to go about the next part in the same column.

 

thanks

PROC Star
Posts: 1,095

Re: Converting the numeric part of a numeric variable into its fraction

First, a question.

 

Is your variable a numeric variable with the % sign applied using a format? Or is it a character variable?

 

Tom

Contributor
Posts: 24

Re: Converting the numeric part of a numeric variable into its fraction

THanks Tom.

My variable looks like :
international_students
25%
27%
33%
22%
And the informat for it is best32.

Now I want to have column that would look like its corresponding fractions:

.25
.27
.33
.22

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PROC Star
Posts: 1,095

Re: Converting the numeric part of a numeric variable into its fraction

If you have a best32. informat, that means that your variable is numeric.

 

In that case, you should be able to get your desired appearance by changing the format.

 

This should work nicely:

 

proc datasets lib=work nolist;

modify have;

format TestVar 5.2;

run;

 

Tom

 

 

Contributor
Posts: 24

COnverting the numeric part of a numeric variable into its corresponding fraction.

HI,

 

I have a dataset of 14 columns.

One of those variables show the percentage of students. e.g 25%

I need to remove the % and replace it with it's corresponding fraction. e.g 25/100 = .25

 

Please let me know if I can do this together.

 

However, what I have doen is just stripped the % from the column. How to go about the next part in the same column.

 

thanks

Respected Advisor
Posts: 3,777

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

[ Edited ]

Antaralib wrote:

HI,

 

I have a dataset of 14 columns.

One of those variables show the percentage of students. e.g 25%

I need to remove the % and replace it with it's corresponding fraction. e.g 25/100 = .25

 

Please let me know if I can do this together.

 

However, what I have doen is just stripped the % from the column. How to go about the next part in the same column.

 

thanks


If we assume the variable is character and not numeric being displayed with PERCENT format you can read it using the INPUT function into a numeric variable with value .25.

 

40         data _null_;
41            pct = '25%';
42            pct2 = input(pct,percent.);
43            put _all_;
44            run;

pct=25% pct2=0.25 _ERROR_=0 _N_=1

 

Contributor
Posts: 24

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Thanks for the prompt response. However the variable is in best32. format.

I tried :

data _null_;
41            pct = '25%';
42            pct2 = input(pct,percent.);
43            put _all_;
44            run;

with the parameters actually in question. Although the log shows  

pct=25% pct2=0.25 _ERROR_=0 _N_=1

I don't see the column with the value 0.25. It still shows 25. I need it to reflect that fraction.

Thanks 

Respected Advisor
Posts: 3,777

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

If the variable you have is numeric and has format best32 then it will not display with a percent sign.  Please show output from PROC CONTENTS for the variable in question and show it looks with PROC PRINT.

 

Also my program was and example not the exact code you need to execute. Especially with new information the variable is numeric.

 

 

 

Respected Advisor
Posts: 3,777

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Now I see you do mention numeric variable in the title of you question.  

 

In that case you simply need to change or remove the associated format which must now be PERCENT. or PERCENTN.

 

Do you know how to change a format?

Contributor
Posts: 24

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Thanks for the response.

 

Do you mean changing the format in the infile statement?

Or,


Storing the percent in the numeric value?

 

Although, I need a separate/same column variable that would show the fractions of the percentages.

e.g instead of 25%

                     33%,

 

it should show 

     .25

     .33

 

Thanks 

Antara

Super User
Posts: 17,868

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Although both @TomKari and @data_null__ have posted correct solutions, you can also fix this in your import process, if you're using an infile statement to read the code. 

 

Leave the informat as is, and add a format statement in the data step to format the data as it's read in. 

If you can't figure it out, or it doesn't work post your full code.

 

Something like the following would work:

 

format variable best8.;
Contributor
Posts: 24

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Thanks. APpreciate the help.

 

However,

 

proc datasets lib=dm nolist;
modify timeseries_1;
format international_Students 5.2;
run;

 

or 

 

proc datasets lib=dm nolist; (dm is my lib name and timeseries_1 is my file name)
modify timeseries_1;
format international_Students best8.;
run;

 

 

gives me upto 2 decimal places.  eg. 25.00, 33.00.

 

But I want the fraction .25, .33 etc.

 

Contributor
Posts: 24

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Even,

format international_Students FRACT2.;

did not work.

Contributor
Posts: 24

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Hey all,

 

I want to crrect one error.

 

If the variable data value is 25%, I need it to display 1/4 and not .25.

 

Apologies.

Super User
Posts: 17,868

Re: COnverting the numeric part of a numeric variable into its corresponding fraction.

Use the FRACTw. format. 

 

Format variable fract8.;

 

Ask a Question
Discussion stats
  • 21 replies
  • 655 views
  • 2 likes
  • 5 in conversation