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
Pyrite | Level 9

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
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-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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