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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1011 views
  • 2 likes
  • 3 in conversation