/* Create a SAS data set containing words to be looked for and */
/* their associated variables. */
data mylist;
input @1 lookfor $32.
@34 varname $32. ;
datalines4;
PHYSICAL physcomp
ACTIVITIES makeract
GAME gamedesign
DATA datasci
CODING coding
MOBILE mobiledev
;;;;
/*** Method 1 ***/
/* Create global macro variables containing expanded parts of array statements. */
%global lookfor_list varname_list list_length;
proc sql noprint;
select
quote(trim(lookfor), "'") ,
trim(varname)
into
:LOOKFOR_LIST separated by ' ' ,
:VARNAME_LIST separated by ' '
from
mylist;
%let list_length = &SQLOBS. ;
quit;
%put INFO: &=LOOKFOR_LIST ;
%put INFO: &=VARNAME_LIST ;
%put INFO: &=LIST_LENGTH ;
data _null_;
array keywords [&LIST_LENGTH.] $32 _temporary_ (&LOOKFOR_LIST.) ;
array varnames [&LIST_LENGTH.] 4 &VARNAME_LIST. ;
put keywords[1]=;
run;
%symdel lookfor_list varname_list list_length;
/*** Method 2 ***/
/* create global macro variables containing complete array statements. */
%macro make_code();
%global lookfor_code varname_code;
%local lookfor_list varname_list list_length;
proc sql noprint;
select
quote(trim(lookfor), "'") ,
trim(varname)
into
:LOOKFOR_LIST separated by ' ' ,
:VARNAME_LIST separated by ' '
from
mylist;
%let list_length = &SQLOBS. ;
quit;
%let lookfor_code = array keywords [&LIST_LENGTH.] $32 _temporary_ (&LOOKFOR_LIST.) ;
%let varname_code = array varnames [&LIST_LENGTH.] 4 &VARNAME_LIST. ;
%put INFO: &=LOOKFOR_CODE;
%put INFO: &=VARNAME_CODE;
%mend make_code;
%make_code();
data _null_;
&LOOKFOR_CODE. ;
&VARNAME_CODE. ;
put keywords[1]=;
run;
%symdel lookfor_code varname_code;
/*** Method 3 ***/
/* Create a macro that inserts code generated from SAS data set, */
/* without causing a section break. In real life, this would be */
/* parameter-driven. */
%macro use_dosub();
%local lookfor_code varname_code;
%local lookfor_list varname_list list_length;
%let dosubl_rc = %sysfunc(dosubl(%nrstr(
proc sql noprint;
select
quote(trim(lookfor), "'") ,
trim(varname)
into
:LOOKFOR_LIST separated by ' ' ,
:VARNAME_LIST separated by ' '
from
mylist;
%let list_length = &SQLOBS. ;
quit;
%let lookfor_code = array keywords [&LIST_LENGTH.] $32 _temporary_ (&LOOKFOR_LIST.) ;
%let varname_code = array varnames [&LIST_LENGTH.] 4 &VARNAME_LIST. ;
)));
%put INFO: RC from &=DOSUBL_RC;
%put INFO: &=LOOKFOR_CODE;
%put INFO: &=VARNAME_CODE;
&LOOKFOR_CODE;
&VARNAME_CODE;
%mend use_dosub;
data _null_;
%use_dosub();
put keywords[1]=;
run;
%put _user_;
This is using a sledgehammer for a penny nail, but sometimes the ability to create code inline without generating a section break is very useful.
... View more