[SASTechies] Put distinct values of a variable in a SAS dataset to a macro variable

12 views
Skip to first unread message

LearnSAS

unread,
Oct 16, 2014, 5:20:13 PM10/16/14
to saste...@googlegroups.com


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(&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;

%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
Reply all
Reply to author
Forward
0 new messages