DATA Step, Macro, Functions and more

Extract words from a string in one macro variable & place them into separate macro variables

Reply
Regular Contributor
Posts: 190

Extract words from a string in one macro variable & place them into separate macro variables

%macro putlog (varlist=);

%local i nextword;

%do i=1 %to %sysfunc(countw(&varlist));

%let nextword = %scan(&varlist, &i);

 

 

this readind the string how can i place the extracted string /word into a new macro variable?

Regular Contributor
Posts: 190

Re: Extract words from a string in one macro variable & place them into separate macro variables

%macro maclist(varlist=);
%local i nextword;
%do i=1 %to %sysfunc(countw(&varlist));
%let nextword = %scan(&varlist, &i);
%put &nextword = &&&nextword;
%end;
%mend maclist;
%let rrt=rahul;
%let p=priya;
%maclist (varlist=rrt p);

this my macro to read the (varlist=rrrt p) now how can i place varlist into new macro variable?
Trusted Advisor
Posts: 1,118

Re: Extract words from a string in one macro variable & place them into separate macro variables

Since VARLIST is already a macro variable, I guess you want to store the concatenated content of the macro variables in &VARLIST in a new macro variable, say, NAMELIST (which would contain rahul priya in your example). This would require only a minor extension of your macro:

 

%macro maclist(varlist=);
  %local i nextword namelist;
  %let namelist=;
  %do i=1 %to %sysfunc(countw(&varlist));
    %let nextword = %scan(&varlist, &i);
    %put &nextword = &&&nextword;
    %let namelist=&namelist &&&nextword;
  %end;
  %put &namelist;
%mend maclist;

If you wanted to use &NAMELIST outside of the macro, you would have to remove namelist from the %LOCAL statement and use a %GLOBAL namelist; statement instead.

 

 

Regular Contributor
Posts: 190

Re: Extract words from a string in one macro variable & place them into separate macro variables

Posted in reply to FreelanceReinhard
@freelance expected output is 2 new macro variables:
Var1 = rahul
Var2 = priya
Regular Contributor
Posts: 190

Re: Extract words from a string in one macro variable & place them into separate macro variables

@freelance when i pass a string to maclist macro then each word in that string should be stored in the separate macro variables
Super User
Super User
Posts: 7,079

Re: Extract words from a string in one macro variable & place them into separate macro variables

First, don't do it. You already have the values in the list, just use the list.

But if you really need to do it and reference the new variables after the macro ends then you need to do something to make sure the new variables are not defined as local to the macro that is creating them. Adding a %GLOBAL statement is one way.

%macro parselist(varlist);
%local i nextvar;
%do i=1 %to %sysfunc(countw(&varlist));
  %let nextvar=word&i;
  %global &nextvar;
  %let &nextvar= %scan(&varlist, &i);
%end;
%mend parselist;
%parselist(A B);
%put &word1 &word2;
Ask a Question
Discussion stats
  • 5 replies
  • 562 views
  • 0 likes
  • 3 in conversation