BookmarkSubscribeRSS Feed
RTelang
Fluorite | Level 6

%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?

5 REPLIES 5
RTelang
Fluorite | Level 6
%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?
FreelanceReinh
Jade | Level 19

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.

 

 

RTelang
Fluorite | Level 6
@freelance expected output is 2 new macro variables:
Var1 = rahul
Var2 = priya
RTelang
Fluorite | Level 6
@freelance when i pass a string to maclist macro then each word in that string should be stored in the separate macro variables
Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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