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

I have a dataset that has one field where the values are

2017_12on15

2018_12on15

2019_12on12

2020_3on3

and then some other values that don't fit this pattern.  I want to select these values into macro variable but I can't seem to get it to work.

Here is what I have that isn't working.  Probably something simple that I am missing.  Any insight would be appreciated.

proc sql noprint; select FIELD1 into :MACRO_VAR separated by "|" from work.HAVE where FIELD1 like '20__\_%on%'; quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input field1 $20.;
datalines;
2017_12on15
2018_12on15
2019_12on12
2020_3on3
;

proc sql noprint; 
   select FIELD1 into :MACRO_VAR separated by "|" 
   from work.HAVE where prxmatch('/\d{4}_\d+on\d+/', FIELD1); 
quit;

%put &MACRO_VAR;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input field1 $20.;
datalines;
2017_12on15
2018_12on15
2019_12on12
2020_3on3
;

proc sql noprint; 
   select FIELD1 into :MACRO_VAR separated by "|" 
   from work.HAVE where prxmatch('/\d{4}_\d+on\d+/', FIELD1); 
quit;

%put &MACRO_VAR;
GeorgeBonanza
Quartz | Level 8
This works. Thanks very much.
Reeza
Super User

You used underscores instead of % for your wild cards. 

 

'20%\%on%'
GeorgeBonanza
Quartz | Level 8

I appreciate you taking the time to respond. 

 

I thought "_" was the wildcard for a single character so if the pattern always starts "20##_", followed by either 1 or 2 digits, then the word "on", and finally another 1 or 2 digits, then wouldn't "__\_" capture the "##" and the underscore in "20##_"?  Then I would need to use % because the next piece can be 1 or 2 digits, similar to after "on" can be 1 or 2 digits.

 

Putting it all together I thought like '20__\_%on%' would work unless "\_%" is escaping both the underscore and %? 

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
  • 5 replies
  • 2554 views
  • 1 like
  • 3 in conversation