How to use SAS Macro call in a %let -


i want use macro in %let call, below macro code , how want invoke it. please me achieve it.

%macro xscan(string, delimiter, word_number);  %let len1=%length(&string); /*computing length of string*/ %let len=%eval(&len1+1); %let sub=%scan(&string,&word_number,"&delimiter"); /*fetch string specified     word_number*/  %if &word_number ge 0 %then %do; %let pos=%index(&string,&sub); /* locate position while reading left right*/ %end;  %if &word_number lt 0 %then %do; data _null_; pos=find("&string","&sub",-&len); /* locate position while reading right left*/ call symput("pos",pos); run; %end;  %let strg=%substr(&string,&pos); /* extract substring*/  %put string &strg; %mend;  %let sub_str = %xscan(a bb ccc dddd bb eeeee, %str( ), -2); %put value of sub_str = &sub_str; 

desired implementation:

 data work.in_data; length in_string $50; in_string = “a bb ccc dddd bb eeeee”;  output; in_string = “aa b cc aa dee”;  output;   run;    data work.out_data; set work.in_data; length sub_str $50; start_word_num = -(_n_ +1); sub_str = %xscan(in_string,’ ‘, start_word_num);   run;    proc print; run; 

i'm posting new answer since other answer answers different question.

here, macro intended perform data step techniques, not macro techniques. cannot (easily) use macro edit variable contents; macro intended write sas code, not modify variables. use proc fcmp solve problem, , may if have more time, here's proper solution data step techniques , normal (non-functional) macro.

first, write data step technique accomplish it. messy effective solution. works negative start_word_num; if left or right desired need modification loop parameters. suggest using starting point , improving needs.

data work.out_data; set work.in_data; length sub_str $50; start_word_num = -(_n_ +1); _t = countc(trimn(in_string),' ')+1 countc(trimn(in_string),' ')+start_word_num+2 -1;     sub_str = catx(' ',scan(in_string,_t,' '),sub_str);     put _t= sub_str=; end; put in_string= sub_str=; run; 

now, move loop macro.

%macro xscan(word_num, initial_string, result); &result.=' '; _t = countc(trimn(&initial_string.),' ')+1 countc(trimn(&initial_string.),' ')+&word_num.+2 -1;     &result. = catx(' ',scan(&initial_string.,_t,' '),&result.); end; %mend xscan;   data work.out_data; set work.in_data; length sub_str $50; start_word_num = -(_n_ +1); %xscan(start_word_num,in_string,sub_str); put in_string= sub_str=; run; 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -