SAS - String named by value of parameter in a macro

69 Views Asked by At

I have dataset CODE_100 and CODE_200. i want to store into variables FILTER_Code_100 AND FILTER_Code_200 all the fileds code so i can use them for queries.

SHould be something like FILTER_CODE_100 = 'A','B','C','D','E','F','G';

MACRO SINGLE_SEG(FLAG);


SET WORK.CODE_&FLAG. END=EOF NOBS=NOBS;
RETAIN STR_FLAGS;
IF _N_= 1 THEN STR_FLAGS= CAT("'",CODE);
ELSE STR_FLAGS= CATS(STR_FLAGS, "','", CODE);

i have tried already Call SYMPUT(FILTER_CODE_&FLAG.,CATS(STR_FLAG,"'"); Ans also %let FILTER_CODE_&FLAG. = CATS(STR_FLAG,"'");

But not successfully

2

There are 2 best solutions below

0
d r On BEST ANSWER

Solved in this way

%MACRO CODE_TABLE(ListCode);

%GLOBAL Filter_&ListCode.;
  PROC SQL NOPRINT;
       SELECT QUOTE(TRIM(CODE),"'")
       INTO :Filter_&ListCode. SEPARATED BY ', '
       FROM TABELLA_CODE_TABLE WHERE Filter_ = "&ListCode.";
       %put Filter_&ListCode. = &&&ListCode.;
   QUIT;
%MEND CODE_TABLE;

%MACRO allCode(ELENCO);
    DATA _NULL_;
          SET &ELENCO;
          CALL EXECUTE(CATS('%CODE_TABLE(', Filter_, ')'));
    RUN;
%MEND AllCode;
%AllCode(WORK.table_distinct_code);

Thanks to everyone for helping

1
PeterClemmensen On

Welcome :-)

It is rarely a good idea to save values from a SAS data set in a macro variable. Rather, you should use the table values directly in the join or whatever you're doing.

Having said that, the easiest way to do this is using Proc SQL Select into. See a small example below. Should get you going.

proc sql noprint;
   select quote(trim(name), "'") 
   into :names separated by ', '
   from sashelp.class
   ;
quit;

%put &names.;

Result:

'Alfred', 'Alice', 'Barbara', 'Carol', 'Henry', 'James', 'Jane', 'Janet', 'Jeffrey', 'John', 'Joyce', 'Judy', 'Louise', 'Mary', 
'Philip', 'Robert', 'Ronald', 'Thomas', 'William'