BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Good morning,

 

I would like to extract the numbers from the sample dataset 'Have'.   I list the result I would like to get in the 'Want' dataset.

 

data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	UGESTAGE=34 5/7 WKS #
	UGESTAGE=35WKS #
	UGESTAGE=33.4 WKS #
	UGESTAGE= 24 WKS
;
run;

data Want;
      infile datalines delimiter='/';
  input UGESTAGE  ;
datalines;
	34 /
	35 /
	33 /
	24
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

One way :

want=input(prxchange("s/^[^\d]*(\d+).*$/$1/",1,Comments), best.);

View solution in original post

6 REPLIES 6
gamotte
Rhodochrosite | Level 12

Hello,

 

One way :

want=input(prxchange("s/^[^\d]*(\d+).*$/$1/",1,Comments), best.);
ybz12003
Rhodochrosite | Level 12
Super!

But what is '^[^\d]*(\d+).*$/$1'?

And what is '1' in front of the 'Comments'?
gamotte
Rhodochrosite | Level 12
It is a perl regular expression (regexp).

https://perldoc.perl.org/perlre

^ : beginning of the string
$ : end of the string
\d a digit
[^\d] any non digit
[^\d]* any sequence of non-digits
\d+ any non empty sequence of digits
(\d+) => the matching string can be referred to in the replace string with $1 ($2, $3, ... if there are other (...) in the regexp)
.* any sequence of characters

So the command searches for a string begining with any sequence of non digits followed by a sequence
of digits (which is given the alias "$1") followed by any sequence of characters.
The input string is replaced by the matching sequence of digits.

prxchange SAS function allows to use perl regular expressions to search/replace elements in a string.
https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n0r8h2fa8djqf1n1cnenrvm573br.htm...

ybz12003
Rhodochrosite | Level 12
Thank you!
Ksharp
Super User
data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	UGESTAGE=34 5/7 WKS #
	UGESTAGE=35WKS #
	UGESTAGE=33.4 WKS #
	UGESTAGE= 24 WKS
;
run;

data Want;
 set have;
 want=scan(comments,1,,'kd');
run;
ybz12003
Rhodochrosite | Level 12

Somehow, I used PERI code is not working properly.   I changed to use Ksharp's code, it works fine.   Thanks much!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 638 views
  • 2 likes
  • 3 in conversation