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?
%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
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);
%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
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.
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.