Help using Base SAS procedures

How to combine macro values and order tham alphabetically?

Accepted Solution Solved
Reply
Contributor gsk
Contributor
Posts: 25
Accepted Solution

How to combine macro values and order tham alphabetically?

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? 

 

 

 


Accepted Solutions
Solution
‎04-13-2018 11:17 PM
PROC Star
Posts: 8,164

Re: How to combine macro values and order tham alphabetically?

%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


All Replies
Super User
Posts: 6,762

Re: How to combine macro values and order tham alphabetically?

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);

Solution
‎04-13-2018 11:17 PM
PROC Star
Posts: 8,164

Re: How to combine macro values and order tham alphabetically?

%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

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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