0

If I have two macro variables :

&foo. = aaa bbb ccc

&bar. = xxx yyy zzz

How can I 'zip' them to give : &bat. = aaa xxx bbb yyy ccc zzz ?

Please note that this is not asking how to concatenate strings (the output is different from aaa bbb ccc xxx yyy zzz).

JustinJDavies
  • 2,663
  • 4
  • 30
  • 52
  • This is functionally identical to the above question, but further remember to address the bigger question asked in that question: you probably should not have those quotes in your macro variable in the first place. Quotes should rarely be included in macro variables, because it is nearly always easier to add the quotes when you use them rather than beforehand - even if these are intended for consumption in a SQL flavor that requires singles. – Joe Sep 12 '14 at 00:32
  • Joe, I disagree. The post you link concatenates strings, this is a request asking how to interweave them. The output doesn't go `aaa bbb ccc xxx yyy zzz`, which would be equivalent to the post you linked. – JustinJDavies Sep 12 '14 at 06:21
  • Also the quotes you refer to weren't explicitly meant as part of the problem space. I have edited to make that clearer. – JustinJDavies Sep 12 '14 at 06:23
  • I agree that this is absolutely not duplicate. About the question: I don't think SAS is sophisticated enough to have inbuilt function for this but defining a macro to do this should be pretty straightforward. I could post my suggestion if someone re-opens this. – Pekka Sep 12 '14 at 07:12
  • @Joe - I'm guessing this may be my fault as I had assumed people would be familiar with Python `zip()` - see https://docs.python.org/2/library/functions.html#zip (In my mind this functionality is similar because space delimited macro variables seem commonly used as arrays in SAS) – JustinJDavies Sep 12 '14 at 07:18
  • Ah, sorry - that wasn't obvious (while I know some python, zip lists aren't familiar to me). A good reminder not to use language specific terminology in other language questions :) I'll reopen. – Joe Sep 12 '14 at 14:24
  • I would still note that this isn't a good idea for the same reasons though: it's not something SAS supports in-built not due to sophistication but because there's probably a better way to do it. How did those macro variables get created in the first place? – Joe Sep 12 '14 at 14:25
  • I am using these macro variables to populate the arguments of a third-party `%MACRO%`. In this case, `foo` is a list of variables and `bar` is a list of the formats I wish to have applied to them when the macro generates its output (outputs binary files, not SAS tables). I have the mapping stored in a table and I could, in theory, output my own `&foobar.` at the same time I SELECT INTO: `&foo.` and `&bar.` I guess my functional-programming mind wanted to do things this way as it seemed more natural. – JustinJDavies Sep 12 '14 at 14:32

1 Answers1

2

Here's a simple example. It should not be too hard to generalize it from your example to your actual problem.

%let foo = aaa bbb ccc;
%let bar = xxx yyy zzz;

%macro zip();
    %let bat =;
    %do i = 1 %to 3;
        %let bat = &bat. %scan(&foo.,&i.) %scan(&bar.,&i.);
    %end;
    %put &bat.;
%mend zip;
%zip()

Currently it's only wrapped in a macro so that I can use the %do loop. The %put statement is only there so that you can see that it's working.

Jeff
  • 1,787
  • 9
  • 14