1
votes

We can make macro variables via the SAS SQL Procedure, using the syntax

select var into :mvar

But I wonder if there's same way in data step.

I have a dataset.

A    B
===  ===
a1   b1
a2   b2
a3   b3

I can make a macro variable called MA with the below statement.

proc sql noprint;
   select "'"||A||"'" into :MA separated by ","
   from dataset;
quit;

How to do it in Data-step?

1
You can use the quote function, instead of all that bothersome concatenation, by the way, in either sql or data step.Joe

1 Answers

7
votes

Firstly, creating your sample dataset:

data dataset;
infile datalines;
input A  $ B $;
datalines;
a1 b1
a2 b2
a3 b3
;
run;

The step below almost does what your PROC SQL does, using CALL SYMPUT to output a macro variable called MA:

data _NULL_;
  retain amac;
  length amac $35;
  set dataset;
  if _N_ = 1 then amac=a; else amac=cats(amac,',',a);
  put amac=;
  call symputx('MA',amac);
 run;

 %put &MA;