I regularly have to process flags from comma-separated character fields that look like this:
example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";
My current process involves the following steps:
I am wondering whether all of this can be reduced to a single datastep. I have dozens of variable like this and I would like to create a macro that can dynamically create flags for each comma-separated value in one datastep. Something like this:
data tmp;
example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";
%expand_list(example_field);
/* yields the output:
flag_AX
flag_EQ
flag_XX
...
flag_DL
*/
run;
Is this wishful thinking? I understand the timing issues between the data step and the macro compilation steps so I think I may be out of luck. I'm hoping someone can give me a glimmer of hope, though. Maybe something like proc FCMP? The flag doesn't have to be strictly [1,0], it could also be [1, .]
Bad example data so hard to determine if this is correct or not:
data have;
example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";
output;
run;
data want1;
set have;
i=1;
do while (scan(example_field, i, ",") ne "");
var_flip=scan(example_field, i, ",");
value=1;
output;
i+1;
end;
run;
proc transpose data=want1 out=want2 prefix=flag_;
id var_flip;
var value;
run;
Post sample data and sample output I think to help understand the problem.
I think you could at least combine step 2/3 using a proc transpose step instead of macro variables, but would need to see more info to be sure.
Have you looked at the SYMPUT and SYMGET routines to see if they can help you accomplish what you are after. See here for documentation: SAS(R) 9.2 Macro Language: Reference
Yes, thanks, I've looked into those. The problem I'm having is that the variables I want to create derive their names from the values of another field. SYMGET will assign a macro variable value to a variable I have already created, but it won't create the variable for me dynamically.
Bad example data so hard to determine if this is correct or not:
data have;
example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";
output;
run;
data want1;
set have;
i=1;
do while (scan(example_field, i, ",") ne "");
var_flip=scan(example_field, i, ",");
value=1;
output;
i+1;
end;
run;
proc transpose data=want1 out=want2 prefix=flag_;
id var_flip;
var value;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.