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
Hello, I would like to extract numbers after text 'UGESTAGE'. Please advice, thank you. BTW, I haven't visited the community for a while, why I couldn't use the insert SAS code function. I apologize if the codes are not shown properly below. data Have; infile datalines delimiter='#'; input Comments : $200. ; datalines; UGESTAGE=34 5/7 WKS # UPREM, UGESTAGE = 32WKS # UGESTAGE =33.4 WKS # GASTROJEJUNOSTOMY DEPENDENCE, BILATERAL GRADE 4 VU ; run; data Want; infile datalines delimiter='/'; input UGESTAGE ; datalines; 34 / 32 / 33 / ; run;
1 ACCEPTED SOLUTION

Accepted Solutions
ybz12003
Rhodochrosite | Level 12
data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	UGESTAGE=34 5/7 WKS #
	UPREM, UGESTAGE = 32WKS #
	UGESTAGE  =33.4 WKS #
	GASTROJEJUNOSTOMY DEPENDENCE, BILATERAL GRADE 4 VU
;
run;

data Want;
      infile datalines delimiter='/';
  input UGESTAGE  ;
datalines;
	34 /
	32 /
	33 /
	
;
run;

I use Chrome, all the function icons are shown again.  I think the previous IE was too old.

View solution in original post

8 REPLIES 8
ballardw
Super User

You should be able to post program code in either a text box or code box opened by clicking on the <> or "running man" icons that appear above the message window (7th and 8th from left to right).

It is possible this is what you meant/wanted to post. Is the first data step correct? or should there be 3 lines of USESTAGE=?

data Have;
 infile datalines delimiter='#';
 input Comments : $200. ;
 datalines;
 UGESTAGE=34 5/7 WKS # UPREM, UGESTAGE = 32WKS # UGESTAGE =33.4 WKS # GASTROJEJUNOSTOMY DEPENDENCE, BILATERAL GRADE 4 VU 
;
 run;
 data Want;
 infile datalines delimiter='/';
 input UGESTAGE ;
 datalines;
 34 / 32 / 33 / 
;
 run;
 

Are you sure that Want data set contains what you really want? It only has the value of 34 for one record.

HB
Barite | Level 11 HB
Barite | Level 11

I'm really not good at all with RegEx but I would like to be. This may work with some of your data until you have a different pattern that breaks it.  But maybe it will get you started. 

 

data extracted;
	set have;
	pattern = prxparse ("/(?<=[=|= ])\d\d(\.[0-9]{1,2})?/");
	call prxsubstr (pattern, comments, start, length);
		if start gt 0 then do;
			ugestage = substr(comments, start, length);
			output;
		end;
	keep ugestage;
run;

proc print data =  extracted;
run;

 

gives me 


Obs ugestage
1 34
2 32
3

33.4

 
 
Edit:  Ballard's question about 33.4 is very important. 
I updated the RegEx to return 33.4
ybz12003
Rhodochrosite | Level 12
Sorry, I don't see any <> or 'running man' icon.
ybz12003
Rhodochrosite | Level 12
data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	UGESTAGE=34 5/7 WKS #
	UPREM, UGESTAGE = 32WKS #
	UGESTAGE  =33.4 WKS #
	GASTROJEJUNOSTOMY DEPENDENCE, BILATERAL GRADE 4 VU
;
run;

data Want;
      infile datalines delimiter='/';
  input UGESTAGE  ;
datalines;
	34 /
	32 /
	33 /
	
;
run;

I use Chrome, all the function icons are shown again.  I think the previous IE was too old.

Tom
Super User Tom
Super User

Do your example strings really start with four leading spaces like you have in your lines of in-line data?

If so your input statement that is using the normal $ informat will remove those leading spaces.  To preserve the leading spaces you need to read the lines using $CHAR.  Or copy the values from the _INFILE_ automatic variable after executing the INPUT statement.

Ksharp
Super User
data Have;
 infile datalines delimiter='#';
 input  @'UGESTAGE' temp : $200. @@ ;
 UGESTAGE=scan(temp,1,'.','kd');
 datalines;
	UGESTAGE=34 5/7 WKS #
	UPREM, UGESTAGE = 32WKS #
	UGESTAGE  =33.4 WKS #
	GASTROJEJUNOSTOMY DEPENDENCE, BILATERAL GRADE 4 VU
;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 675 views
  • 2 likes
  • 6 in conversation