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

Let's say I have (numlist,datelist, charlist are all variable lists): 

 

%put &numlist;

a  c e
%put &datelist;

b d 
%put &charlist;

f z

 

I want to combine a, c, e, b, d, f, z and make an output of a string that has values of a b c d e f z so that I could feed this string into a macro to do a set of analysis on each of these variables. 

 

How do I combine &numlist, &datelist, &charlist and order the values alphabetically? 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
%let numlist=a c e;
%let datelist=b d;
%let charlist=f z;
data test;
  length lists $255;
  array list(7) $;
  lists=catx(' ',"&numlist.", "&datelist.","&charlist.");
  i=1;
  do while (scan(lists,i) ne '');
    list(i)=scan(lists,i);
    i+1;
  end;
  call sortc(of list(*));
  call symput('alllist',catx(' ',of list(*)));
run;
%put &alllist.;

Art, CEO, AnalystFinder.com

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

While SAS handles this much more easily than macro language, here's how you might approach the problem using macro language.  I can't test this right now, so I'm hoping there are no glitches (always mildly dangerous using %DO %UNTIL):

 

%macro reorder (list=);

   %* Break up the list into separate macro variables;

   %local n_vars i;

   %let n_vars = %sysfunc(countw(&list));

   %do i=1 %to &n_vars;

       %local temp&i;

       %let temp&i = %scan(&list, &i);

   %end;

   %* Order the macro variables;

   %local changes j tempvar;

   %do %until (&changes=N);

      %let changes=N;

      %do i=2 %to &n_vars;

         %let j = %eval(&i - 1);

         %if &&temp&i < &&temp&j %then %do;        

             %let tempvar = &&temp&j;

             %let temp&j = &&temp&i;

             %let temp&i = &tempvar;

             %let changes = Y;

         %end;

      %end;

   %end;

   %* Spit out the reordered values;

   %do i=1 %to &n_vars;

       &&temp&i

   %end;

 

%mend;

 

This returns a list of variables in the proper order.  So you would need to call in in a program that expects to see such a list.  For example:

 

%put %reorder (list=&numlist &datelist &charlist);

 

or

 

%let newlist = %reorder (list=&numlist &datelist &charlist);

art297
Opal | Level 21
%let numlist=a c e;
%let datelist=b d;
%let charlist=f z;
data test;
  length lists $255;
  array list(7) $;
  lists=catx(' ',"&numlist.", "&datelist.","&charlist.");
  i=1;
  do while (scan(lists,i) ne '');
    list(i)=scan(lists,i);
    i+1;
  end;
  call sortc(of list(*));
  call symput('alllist',catx(' ',of list(*)));
run;
%put &alllist.;

Art, CEO, AnalystFinder.com

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 787 views
  • 2 likes
  • 3 in conversation