I have this sample data set:
data WORK.CheckSet;
input Col1 $ Col2 $ Col3 $ Col4 $ Col5 $;
datalines;
X A B C D
Y D C F G
Z F B H E
;
run;
What I'm trying to do is test each column variable against an equal amount of test variables testvar1..5 So I also have:
%let testVar1 = A;
%let testVar2 = B;
%let testVar3 = C;
%let testVar4 = D;
%let testVar5 = E;
I wanted to practice using an array so this is where I'm getting stuck:
data WORK.ResultSet;
set CheckSet;
Array Result[*] Col1-Col5;
/*
<paudocode starts here>
Res[1] = if &testvar1. = Col1 then 1 else 0
Res[2] = if &testvar2. = Col2 then 1 else 0
Res[3] = if &testvar3. = Col3 then 1 else 0
Res[4] = if &testvar4. = Col4 then 1 else 0
Res[5] = if &testvar5. = Col5 then 1 else 0
<psudocode ends here>
*/
run;
I'm just not sure how to tell SAS to generate 5 new variables as it loops through the array...
my desired result is:
Thanks!
You need TWO arrays. One of the OLD variables and one for the NEW variables.
data CheckSet;
input (Col1-Col5) ($);
datalines;
X A B C D
Y D E F G
Z F B H A
;
%let testval = A ;
data want;
set CheckSet;
array old col1-col5 ;
array new res1-res5 ;
do index=1 to dim(old);
new[index] = old[index] = "&testval" ;
end;
drop index ;
run;
Result
Obs Col1 Col2 Col3 Col4 Col5 res1 res2 res3 res4 res5 1 X A B C D 0 1 0 0 0 2 Y D E F G 0 0 0 0 0 3 Z F B H A 0 0 0 0 1
You need TWO arrays. One of the OLD variables and one for the NEW variables.
data CheckSet;
input (Col1-Col5) ($);
datalines;
X A B C D
Y D E F G
Z F B H A
;
%let testval = A ;
data want;
set CheckSet;
array old col1-col5 ;
array new res1-res5 ;
do index=1 to dim(old);
new[index] = old[index] = "&testval" ;
end;
drop index ;
run;
Result
Obs Col1 Col2 Col3 Col4 Col5 res1 res2 res3 res4 res5 1 X A B C D 0 1 0 0 0 2 Y D E F G 0 0 0 0 0 3 Z F B H A 0 0 0 0 1
Explain how the macro variables are used (explain in words, show us the output, and we'll help create the code)
That could work.
It will create 5 additional variables each of which has a constant value for all observations.
If you do not need those extra variables then define the array a temporary.
Also note you don't have to use comma as the delimiters in the list of initial values. Spaces work just as well and are a lot easier to type. And much much easier to use if you want to pass the values in macro variables.
array testval[5] $ _temporary_ ("X" "A" "C" "D" "E");
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.