In Stata you can loop over a list of character values with the foreach command. I've been trying to do the same in SAS to no avail so far. I'm trying to run a series of data and proc statements over all the values of character column. I tried with the following:
%let mylist = a b c; * These are all the values of a column called "code";
data mydata_@mylist; * Each element creates a new table;
set mydata;
where code=&mylist;
run;
What am I doing wrong or missing?
Thanks in advance,
Matías
This version uses
COUNTW
andSCAN
functions with modifiers to only use space (%str( )
) as a word separator.Also uses new macro
getName
to compose a name for datasets according to SAS naming rules (note%let name = ;
to simply provide variable inside%loopit
for%getName
to fill).%getName
translates not allowed characters to underscore (a potential name conflict here if you have same values with different separator).Try this:
Modified version based on what DomPazz provided:
Here I process input data only once (for efficiency if you need) creating all output tables in one data step. Also I'm creating an "Other" table. To process only records with code from list is desired, you can add WHERE statement under SET statement and omit else output other;