Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created January 19, 2021 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save statgeek/097840d38bdbb77dbaabd6f1ffec1d1e to your computer and use it in GitHub Desktop.
Save statgeek/097840d38bdbb77dbaabd6f1ffec1d1e to your computer and use it in GitHub Desktop.
SAS - convert macro variable space delimited list to comma delimited
*An example/demo of how to convert a macro variable list that is space delimited to comma delimited.
%Let weightvar = WEIGHT HEIGHT AGE;
*uses the translate function to replace spaces with commas. If VALIDVARNAME=ANY this may not work;
%macro addCommas(varList=);
%sysfunc(translate(&varList, %str(, ), %str( )))
%mend addCommas;
*example recoding;
%let comma_delimited_list = %addCommas(varList = &weightvar);
%put &comma_delimited_list;
*example of in line usage within a procedure or data step;
data class;
set sashelp.class;
useless_variable = sum(%addCommas(varList = &weightVar.));
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment