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!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1046 views
  • 2 likes
  • 3 in conversation