I have a data set, created from an Excel file, where logic is stored in a character variable named additional_logic. I am trying to resolve that logic in another variable--e.g. in the below example, TrueFlase would equal 0 or 1. I thought this would be simple, but I can't seem to find anything that works. I've tried IFN, I've tried CALL SYMPUTX + SYMGET to no avail. Any guidance is appreciated.
data x;
length additional_logic $50;
input additional_logic $;
datalines;
put(today(),day.)=6
put(today(),day.)^=6
;
data _null_;
set x;
call symputx('additional_logic',additional_logic);
TrueFalse=symget('additional_logic');
put TrueFalse;
run;
You can use DOSUBL like the example. However it is usually better to write code that will compile the logic in a "regular" data step. But DOSUBL is fun so...\
data x;
length additional_logic $50;
input additional_logic $;
datalines;
day(today())=6
day(today())^=6
;;;;
%global additional_logic;
data tf;
set x;
todo=catx(' ','data _null_; call symputx("additional_logic",',additional_logic,'); stop; run;');
rc = dosubl(todo);
TrueFalse=symgetn('additional_logic');
put _all_;
run;
%put _user_;
You can use DOSUBL like the example. However it is usually better to write code that will compile the logic in a "regular" data step. But DOSUBL is fun so...\
data x;
length additional_logic $50;
input additional_logic $;
datalines;
day(today())=6
day(today())^=6
;;;;
%global additional_logic;
data tf;
set x;
todo=catx(' ','data _null_; call symputx("additional_logic",',additional_logic,'); stop; run;');
rc = dosubl(todo);
TrueFalse=symgetn('additional_logic');
put _all_;
run;
%put _user_;
The easiest way to apply these pieces of logic is to split the processing: one DATA step to bring in the formulas, and a second DATA step to apply them. Here is a simplified version (since it doesn't automate the number of formulas to apply):
data _null_;
length additional_logic $50;
input additional_logic $;
call symputx(cats('v', _n_), additional_logic);
datalines;
put(today(),day.)=6
put(today(),day.)^=6
;
data want;
set have;
TrueFalse1 = &v1;
TrueFalse2 = &v2;
run;
I find it personally often easiest to first generate the dynamic code into a file and then %include the file.
data fromExcel;
length additional_logic $50;
input additional_logic $;
datalines;
day(today())=6
day(today())^=6
;;;;
filename codegen temp;
data _null_;
file codegen;
/* file print;*/
set fromExcel;
put 'TrueFalse_' _n_ z3. '=' additional_logic ';';
run;
data want;
set sashelp.class;
%include codegen / source2;
run;
filename codegen clear;
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.