/* If Variable contains a certain string,
give Target the same value as the macro variable (whose name has the same string) has */
%let A_value = 1;
%let B_value = 2;
...
%let Z_value = 26;
%let A_count = 1.1;
%let B_count = 2.2;
...
%let Z_count = 26.26;
%macro test;
%do _j=1 %to 5;
data test_&_j;
set train_&_j;
if find(Variable, 'A') ge 1 then Target = &A_value;
if find(Variable, 'B') ge 1 then Target = &B_value;
...
if find(Variable, 'Z') ge 1 then Target = &Z_value;
run;
%end;
%mend test;
%test
run;
As you can see, the codes above are not efficient but hardcoded. Is there a more generic approach for the same purpose?
Thank you.
Sounds like you are looking for something like the following:
%macro test; %do _j=1 %to 5; data test_&_j; set train_&_j; Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_'))); run; %end; %mend test; %test
HTH,
Art, CEO, AnalystFinder.com
I don't completely understand what you're trying to do, but if the letter values A-Z contain 1-26, and always will, this should work (since we don't have access to your train_ datasets. (Assuming you're using ASCII - different values for EBCDIC!)
%macro test;
%do _j = 1 %to 1;
data test_&_j;
set train_&_j;
do i = 65 to 90;
if not find(variable, byte(i)) then
continue;
target = i - 64;
leave;
end;
run;
%mend test;
%test;
65 to 90 are the ASCII values for A-Z; byte(65) returns A.
indexc will work as well as find, here.
Hi Laurie, thanks for your reply.
So basically there is one column in the original dataset, named 'Variable'. It has different character values, e.g. Apple, Banana, Carrot. Several macro variables have been created, e.g. Apple_MV (has a value of 100, numeric), Banana_MV (has a value of 200, numeric).
I want to add a new column to the original dataset, named 'Target'. For each row, if it is 'Apple' in Column 1, then the value in Column 2 is 100 (because Apple_MV = 100). if 'Banana' in Column 1, then the value in Column 2 is 200 (because Banana_MV = 200).
What do the values of variable look like and what is the variable's length?
Art, CEO, AnalystFinder.com
So the values of column 1, Variable, are just simple English words, e.g. Apple, Banana. The type of this column is Character, and length (in bytes) is 3.
The macro variables could have names like 'Apple_value', 'Banana_value', 'Apple_count', and we are only getting the values from macro variables whose name ends with '_value' instead of '_count' here.
Sounds like you are looking for something like the following:
%macro test; %do _j=1 %to 5; data test_&_j; set train_&_j; Target = symget(catt(scan(Variable, 1,'_'),"_",scan(Variable, 2,'_'))); run; %end; %mend test; %test
HTH,
Art, CEO, AnalystFinder.com
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.