BookmarkSubscribeRSS Feed
Antaralib
Obsidian | Level 7

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

21 REPLIES 21
TomKari
Onyx | Level 15

First, a question.

 

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

 

Tom

Antaralib
Obsidian | Level 7
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. -##
TomKari
Onyx | Level 15

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

 

 

Antaralib
Obsidian | Level 7

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

data_null__
Jade | Level 19

@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

 

Antaralib
Obsidian | Level 7

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 

data_null__
Jade | Level 19

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.

 

 

 

data_null__
Jade | Level 19

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?

Antaralib
Obsidian | Level 7

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

Reeza
Super User

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.;
Antaralib
Obsidian | Level 7

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.

 

Antaralib
Obsidian | Level 7

Even,

format international_Students FRACT2.;

did not work.

Antaralib
Obsidian | Level 7

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.

Reeza
Super User

Use the FRACTw. format. 

 

Format variable fract8.;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 21 replies
  • 5936 views
  • 2 likes
  • 5 in conversation