BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASAlex101
Quartz | Level 8

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:

SASAlex101_0-1672946024756.png

 

 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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

 

SASAlex101
Quartz | Level 8
Nice solution but you beat me to the punch as I was editing my question... I see the [index] is key here.
Can your solution be adapted for the macro variables 1 to 5 above? or would it be easier to make a third array 1x5 with the test variables in it to loop through?
PaigeMiller
Diamond | Level 26

Explain how the macro variables are used (explain in words, show us the output, and we'll help create the code)

--
Paige Miller
SASAlex101
Quartz | Level 8
Tom! I think this may work:
data want;
set CheckSet;
array old col1-col5 ;
array new res1-res5 ;
array testval[5] $ ("X","A","C","D","E");
do index=1 to dim(old);
new[index] = old[index] = testval[index] ;
end;
drop index ;
run;
Tom
Super User Tom
Super User

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");

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2039 views
  • 3 likes
  • 3 in conversation