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
Opal | Level 21

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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