简体   繁体   中英

SAS, define macro variable

I need to define the following variables. the user need to specify the input variables in group, they can be 1, 2, or 3 variables like A, or A, B, or A, B, C. Now they also need to manually specify group_2 and group_3. But as you can see, as long as group input is fixed, then group_2 and group_3 are also fixed.

Is there any way to make the macro variable input more concise (user just need to input group, then group_1 and group_2 will automatically generated)?

%let group = A B;
%let group_2 = A, B;
%let group_3 = A trimmed, : B trimmed;

%let group = A B C;
%let group_2 = A, B, C;
%let group_3 = A trimmed, : B trimmed, : C trimmed;

Presuming the SAS variable names are standard names you can use TRANWRD to transform a space separated list of items into a more complicated form.

compbl replaces repeated spaces with a single space
tranwrd is used to replace the space separating the items with a consistent complexifying injection

%let list = %sysfunc(compbl(&list));
%let list_csv = %sysfunc(tranwrd(&list,%str( ),%str(,)));
%let list_into = :%sysfunc(tranwrd(&list,%str( ),%str( trimmed, :))) trimmed;

%put NOTE: &=list;
%put NOTE: &=list_csv;
%put NOTE: &=list_into;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM