1

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?

andrey_sz
  • 751
  • 1
  • 13
  • 29
raccoonjin
  • 217
  • 1
  • 5
  • 12
  • 2
    You can use the `quote` function, instead of all that bothersome concatenation, by the way, in either sql or data step. – Joe Apr 21 '15 at 16:09

1 Answers1

7

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;
mjsqu
  • 5,151
  • 1
  • 17
  • 21
  • 3
    You could simplify it slightly by replacing the `if, then, else` statement with `call catx(',',amac,a);` – Longfish Apr 21 '15 at 10:06
  • @user3457716: If this sufficiently answers your question, please consider marking it as accepted. – Alex A. Apr 21 '15 at 16:19