BookmarkSubscribeRSS Feed
mansour_ib_sas
Pyrite | Level 9

Hello,

Starting from this subject: https://communities.sas.com/t5/SAS-Programming/How-to-create-columns-with-values-based-on-occurence-...

I wanted to solve it with regular expressions (just a challenge for me). 

Passing parametres seems to me not to work.

Could you help me.

Thank you:

data variables;                                                                                                                         
   infile datalines delimiter=',' dsd;                                                                                                  
   length cars $6 vegetables $12 regions $14;                                                                                           
   input cars $ vegetables $ regions $;                                                                                                 
   datalines;                                                                                                                           
Audi,beans,America                                                                                                                      
BMW,black beans,Central Europe                                                                                                          
BMW X5,carrot,East Europe                                                                                                               
X5,chili pepper,Europe                                                                                                                  
Honda,green beans,North America                                                                                                         
,lettuce,South America                                                                                                                  
,onion,                                                                                                                                 
,pepper,                                                                                                                                
;                                                                                                                                       
run;                                                                                                                                    


data test ;                                                                                                                          
  input var $30.;                                                                                                                    
  cards ;                                                                                                                               
Ford carrot                                                                                                                             
Honda beans                                                                                                                             
south america black beans                                                                                                               
green beans Audi                                                                                                                        
x5 chili pepper lettuce                                                                                                                 
garlic north America                                                                                                                    
central europe bmw                                                                                                                      
lettuce onion X5                                                                                                                        
America pepper                                                                                                                          
X5 BMW Europe                                                                                                                           
Central East Europe                                                                                                                     
bmw X5 america                                                                                                                          
;                                                                                                                                       
run ;  

                                                                                                                                 
                                                                                                                                        
proc sql; 
select distinct cars , length(cars) as maxcar into: cars separated by '|'  
from variables where cars ne "" order by   maxcar desc;

select distinct vegetables , length(vegetables) as maxvege into: vege separated by '|'  
from variables where vegetables ne "" order by   maxvege desc;

select distinct regions , length(regions) as maxregion into: region separated by '|'  
from variables where regions ne "" order by   maxregion desc;

%put &cars;
%put &vege;
%put &region;
        
option mlogic mprint ; 

%macro tt(); 
data test1 (drop=patt: START LENGTH);
set test;

 %let PATTERN1 =  %sysfunc(PRXPARSE("/&cars/i"));
  %let PATTERN2 =  %sysfunc(PRXPARSE("&vege/i"));
   %let PATTERN3 =  %sysfunc(PRXPARSE("/&region/i"));
  
length var1 $8;

%do i=1 %to 3;
 %syscall PRXSUBSTR (&&PATTERN&i,var,START,LENGTH));
    %IF START GT 0 %THEN %DO;
    var&i = SUBSTR(var,START,LENGTH);
    %end;
  %else %do;
     var&i="";
  %end;
 %end;
 output;
run;
%mend tt;
%tt;
1 REPLY 1
Tom
Super User Tom
Super User

You don't seem to understand the difference between macro code and SAS code. In particular this statement makes no sense.

%IF START GT 0 %THEN %DO;

A five character string that starts with the letter S is always going come after the digit 0 in the lexical ordering used for character strings.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 612 views
  • 1 like
  • 2 in conversation