SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
VirginiaGo
Calcite | Level 5

Could anyone tell me what's wrong with this program?

 

data a.new1;
set a.new;
n1=0;
array k1 {16} k1get k2get k3get k4get k5get k6get k7get k8get k9get k10get k11get k12get k13get k14get k15get k16get;
do i = 1 to 16;
if k1=1 then n1=n1+1;
end;
drop i;
run;

 

Because an error showed up

"Illegal reference to the array k1."

Thank you.

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

You are not really referencing anything in your array, but your entire array itself. I'm guessing you want to reference the i'th instance of your k1 array, so your line inside your do loop

 

if k1=1 then n1=n1+1;

should be

 

if k1[i]=1 then n1=n1+1;

since right now, you are not using your i variable at all 🙂

 

ballardw
Super User

And a good example of possibly rethinking variable names. If you had named the variables Kget1 to Kget16 or GetK1 to GetK16 you could reference them in the array statement as

 

array k1 Kget1-Kget16; or Array k1 GetK1-GetK16;

or if that is all of the "get" variables

array k1 Kget: ;   or array k1 GetK: ;

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 1200 views
  • 3 likes
  • 3 in conversation