Put distinct values of a variable in a SAS dataset to a macro variable

This post was kindly contributed by SASTechies - go there to comment and to read the full post.

Here’s a macro that can put distinct values of a variable in a SAS dataset to a macro variable of your choice.
%macroput_distinct_varvalues_in_list(dsn,var,mac_var,dlm=comma,quotes=double);
     %global&mac_var.;
     %if&dlm eq comma %then %letdlm=%str(,);
     %if&dlm eq pipe %then %letdlm=%str(|);
     proc sql noprint;
     select DISTINCT
     %if%upcase(&quotes) eq SINGLE %then%do; “‘”||&var.||“‘”%end;
     %else%if %upcase(&quotes) 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;
%mendput_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.

This post was kindly contributed by SASTechies - go there to comment and to read the full post.