SAS Concatenate Multiple Variables to Create Data-Driven Macro Statements

1.4k views Asked by At

In order to keep my process data-driven, I'm trying to concatenate multiple variables, separated by comma, in order to ultimately put them in a PROC SQL list to call in multiple macro statements that would otherwise clutter my SAS pogram.

Take the following sample dataset:

DATA TEST;
  INPUT YEAR CONDITION $ QRTS $12. SAMPLE $;
  DATALINES;
  2008  MI  (1,2,3,4)   A
  2008  MI  (1,2,3,4)   B
  ;
RUN;

I'd like to concatenate these variables together, again separated by comma, and add %APPEND to the front to create a huge list of macro statements. The new string variable, and ultimately the macro statements, would look like:

%APPEND(2008,MI,(1,2,3,4),A);

%APPEND(2008,MI,(1,2,3,4),B);

I've used the following to compile strings of means and confidence intervals, and I would imagine it would also be similar (I just cannot figure out these quotes, commas, and bars with my data):

ci=compress(put(mean,7.2))||"("||compress(lo)||","|| compress(hi)||")";

Once this long string variable is created, I would select this variable into a long list through PROC SQL statement to be something like:

PROC SQL;
    SELECT DISTINCT *NEW LONG STRING VARIABLE*
    INTO: MACROLIST SEPARATED BY ' '
    FROM TEST;
QUIT;

Ultimately, running &MACROLIST. will call in hundreds of macro statements to append multiple datasets into a large master dataset. I appreciate the insight.

1

There are 1 answers

2
kstats9pt3 On BEST ANSWER

Answered my own question. This does what I need:

DATA TEST1; SET TEST;
    STRING=COMPRESS(%NRSTR("%APPEND""("||COMPRESS(YEAR)||","||COMPRESS(CONDITION)||","|| COMPRESS(QRTS)||","|| COMPRESS(SAMPLE)||")"),"""");
RUN;

%APPEND can then be replaced by any macro name created, so this could be very useful.