BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10
data have1;
infile datalines dsd dlm=",";
	input number $;
datalines;
5 (31.4),
3 (12.4),
38 (71.3)
;
run;

Hi, i'm trying to extract then numbers out of the percent and turn them into 

numerical values.

 

The desired result should be:

31.4

12.4

71.3

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way:

data want;
   set have1;
   num = input(scan(number,2,'()'),f5.);
run;

Scan function allows setting what is the delimiter between values of interest so ( and ) seem likely  candidates. The second word would start after the ( and end at the ). Input is the function to turn the value in numeric value with an appropriate informat.

 


@Hello_there wrote:
data have1;
infile datalines dsd dlm=",";
	input number $;
datalines;
5 (31.4),
3 (12.4),
38 (71.3)
;
run;

Hi, i'm trying to extract then numbers out of the percent and turn them into 

numerical values.

 

The desired result should be:

31.4

12.4

71.3

 

Thanks!


 

View solution in original post

5 REPLIES 5
ballardw
Super User

One way:

data want;
   set have1;
   num = input(scan(number,2,'()'),f5.);
run;

Scan function allows setting what is the delimiter between values of interest so ( and ) seem likely  candidates. The second word would start after the ( and end at the ). Input is the function to turn the value in numeric value with an appropriate informat.

 


@Hello_there wrote:
data have1;
infile datalines dsd dlm=",";
	input number $;
datalines;
5 (31.4),
3 (12.4),
38 (71.3)
;
run;

Hi, i'm trying to extract then numbers out of the percent and turn them into 

numerical values.

 

The desired result should be:

31.4

12.4

71.3

 

Thanks!


 

Hello_there
Lapis Lazuli | Level 10
Thank you for the explanation!
DrAbhijeetSafai
Lapis Lazuli | Level 10

Many thanks. This use of scan function has saved lot of my time. It is very useful where substr is not useful because substr is position specific.

 

Thank you.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
awesome_opossum
Obsidian | Level 7
data want; set have;
Newvar = substr(oldvar, 1, 1);
Newvar2 = .;
Newvar2 = Newvar;
run;
Reeza
Super User

Slight variation on @ballardw solution which converts it to an actual percentage.

 

data want;
   set have1;
   num = input(scan(number,1,'()'),f5.);
   pct = input(scan(number,2,'()'),f5.)/100;
   format pct percent12.1;
run;

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 Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 946 views
  • 6 likes
  • 5 in conversation