BookmarkSubscribeRSS Feed
Sowmya12
Fluorite | Level 6
data ss;
input details$33.;
cards;
anam kusuma,3 id 2018, svcp
b bhavana, tirupati, 2018, 011
gracy, tirupati
indu priya, sas, 22, nellore
;
run;

Output is
3
2018
0
22
2 REPLIES 2
andreas_lds
Jade | Level 19

Read your post multiple times, but still i am unable to grasp the rules that should be applied. So please explain them. Also explain, why you want to  use "anyspace".

AMSAS
SAS Super FREQ

I think you want to do something like the example below, using Regular Expressions

 

What the example is doing is looking for a comma, followed by any number and combination of numerics and spaces, followed by another comma. It then extracts that value and converts it into a numeric. 

Be warned this will also pick up values like ", 1 2 3 ," which will then probably return missing values. 


What's not clear from your post is why the output has

  • 3 for the first observation (maybe there's a typo and there's supposed to be a comma between 3 and id)
  • Selects only 2018 for the second observation and not 011

    
    /* Using Perl Regular Expressions in the DATA Step */
    /* https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1vz3ljudbd756n19502acxazevk.htm */ 
    
    data ss;
    input details $33.;
    cards;
    anam kusuma,3 id 2018, svcp
    b bhavana, tirupati, 2018, 011
    gracy, tirupati
    indu priya, sas, 22, nellore
    ;
    run;
    
    data want ;	
    	retain regExpID ;
    	if _n_=1 then do ;
    		regExpID=PRXPARSE('/,[0-9 ]*,/') ;
    	end ;
    	set ss ;
    	prxmatch=prxmatch(regExpID,details) ;
    	if pexmatch then do ;
    		prxposn=prxposn(regExpID,0,details) ;
    		number=inputn(substr(prxposn,2,length(prxposn)-2),"8.") ;
    	end ;
    	else
    		number=0 ;
    	put details= ;
    	put prxmatch = ;
    	put prxposn = ;
    	put number= ;
    	put "-------" ;
    
    run ;	

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 638 views
  • 0 likes
  • 3 in conversation