BookmarkSubscribeRSS Feed
user112a2
Obsidian | Level 7

I am analyzing data. I need to extract everything from these strings before the first space. How can I extract the substring of everything up to the first space. I am using SAS and have used PRXMATCH but not familiar with doing this. Thanks!

 

Removed

 

So in my output I need:

Removed

I then need to extract only the first numbers so I get:

 

Removed


Any help is greatly appreciated. Thanks much

3 REPLIES 3
ballardw
Super User

string = scan(variablename,1,' ');

should do it. You should set the length of the target variable string before use though to a long enough value to hold expected results.

something like

 

length string $ 100;

string = scan(variablename,1,' ');

Ksharp
Super User
data have;
input x $80.;
x1=scan(x,1,' ');
x2=scan(x,1,,'kd');
cards;
0518Audible adbl.co/bill NJ 01
06257-ELEVEN CHICAGO IL Purchase $33.30 Cash Back $10.00
0625#03345 JEWEL CHICAGO IL Purchase $58.58 Cash Back $20.00 00
;
run;
jjsingh04
Obsidian | Level 7

For those who may not have followed what Ksharp has done here:

 

1) The COUNT of 1 in the SCAN function means that we want to get the FIRST word from the string x. Since COUNT is POSITIVE, SCAN scans words from LEFT to RIGHT within the string x. 

 

2) kd is a "SAS Character Function" that retains only numeric values from a string. Similarly, ak is a SAS Character Function that retains only alphabetical values from a string. 

 

So here, kd would give us e.g. 06257 for x2 in the second row. 

 

Had ak been used for x2, it would've given us e.g. ELEVEN instead. 

 

The result for x1 here is 06257-ELEVEN 

Our lives are enriched by the people around us.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 27011 views
  • 5 likes
  • 4 in conversation