Here's a macro that can put distinct values of a variable
in a SAS dataset to a macro variable of your choice.
%macro
put_distinct_varvalues_in_list(dsn,var,mac_var,dlm=comma,quotes=double);
%global
&mac_var.;
%if
&dlm eq comma %then %let
dlm=%str(,);
%if
&dlm eq pipe %then %let
dlm=%str(|);
proc sql noprint;
select DISTINCT
%if
%upcase("es) eq SINGLE %then
%do; "'"||&var.||"'"
%end;
%else
%if %upcase("es)
eq DOUBLE %then %do;
quote(&var.) %end;
%else
&var.;
into :&mac_var separated by "&dlm."
from &dsn;
quit;
%let
&mac_var = &&&mac_var;
%put
&mac_var = &&&mac_var;
%mend
put_distinct_varvalues_in_list;
For e.g.
%put_distinct_varvalues_in_list(sashelp.class,sex,gender);
would assign the values "F","M" to the
macro variable gender.
--
Posted By LearnSAS to
SASTechies at 7/07/2014 05:18:00 PM