2

How can I read a SAS-Dataset with a name given as stem+suffix into IML? The stem is given as a SAS macro variable, the suffices I intend to use are in a string-vector in IML.

In R I would use

suffix<-c('s1','s2')
for (s in suffix){
   data<-eval(as.name(paste(stem,s,sep='')))
}

I could do the looping if I had the code for the first dataset. I tried:

proc iml;
suffices = {'s1','s2'};
call symput('suffix',suffices[1]);
use &stem.&suffix.;

The problem being that if in a do-loop (and I need this as I loop over names), call symput does not really work. Here i found symget, but in the context of use &stem.symget('suffix') was not fruitful.

Any other ideas?

Edit: I found the following rather inelegant solution:

proc iml;
%global suff;
suffix={'s1','s2','s3'};
%do ii = 1 %to 3;
call symput('suff',suffix[&ii.]);
<do stuff based on the suffix>
%end;

Still I do not feel this is the way one is supposed to work on it.

user1965813
  • 671
  • 5
  • 16
  • This shouldn't be difficult, but I don't think I totally understand what you're trying to do here. What are you going to do with the datasets once you use them - are you going to use all of them and then do something, or are you going to use one, do something, and close it? – Joe Jul 18 '13 at 13:37
  • Every Table contains values for certain ages (rows) and generations (columns), plus a shift (indicated by suffix). I want to end up with a table Age||Generation||Age+Generation+Shift||Value, combining the values for all tables (i.e. all shifts) – user1965813 Jul 19 '13 at 05:32
  • Have you looked at my solution? If it's not sufficient, would you mind commenting as to why? Your 'inelegant' solution is fine, although I don't suggest it only because it's putting data into code (the 1/2/3); that should really be in data proper. – Joe Jul 19 '13 at 17:19

2 Answers2

1

The easiest way I can think of to do this is to use some non-IML syntax. PROC SQL for example can generate macro variable lists.

%let stem=class_;
data class_s1 class_s2;
set sashelp.class;
run;

data suffices;
input suffix $;
datalines;
s1
s2
;;;;
run;

%macro use_suffix(suffix=);
use &stem.&suffix.;
read all into &stem.&suffix.;
print &stem.&suffix.;
%mend use_suffix;

proc sql;
select cats('%use_suffix(suffix=',suffix,')') into :suffixlist separated by ' ' from suffices;
quit;

proc iml;
&suffixlist;
quit;
Joe
  • 62,789
  • 6
  • 49
  • 67
  • I did not think of generating a list thah holds commands. That was indeed eye-opening. However it did not solve my problem, as as far as I understand it, iml does not know any of the suffices, hence I do not know how to act based on them. (i.e. how would i do a if suffix=a then ... with that approach?) – user1965813 Jul 22 '13 at 06:30
0

If you have SAS/IML 12.1, simply use string concatenation to construct the data set name, and then put parentheses around the name, as described in the blog post "Read data sets that are specified in an array".

Be careful when you try to use macro variables in a loop. See the tips in the article "Macros and loops in the SAS/IML language"

Rick
  • 1,210
  • 6
  • 11