Hello,
I'm using SAS 9.4 edition. I have would like to define several different variables using the same 69 rows of data in the same data step but I believe the array names have to differ. My coding is here:
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
Org_named=0;
do g= 1 to 69;
if DEFENDANT_TYPE [g]='Organization' then Org_named=1;
end;
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
n_defendant=0;
do h= 1 to 69;
if DEFENDANT_TYPE [h] > ' ' then n_defendant + 1;
end;
When I change the name "DEFENDANT_TYPE" to "Org_named" in the first array or "n_defendant" in the second array, I get the following error messages.
ERROR: Illegal reference to the array Paid.
ERROR: Undeclared array referenced: DEF_INDEMNITY_PAID.
ERROR: Variable DEF_INDEMNITY_PAID has not been declared as an array.
ERROR: Illegal reference to the array Paid.
Can someone help?
There is no need to define a second array to scan the same list of variables a second time. In fact there is no need to scan over the array twice for that example. Just put both IF statements in the same DO loop.
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
Org_named=0;
n_defendant=0;
do g= 1 to 69;
if DEFENDANT_TYPE [g]='Organization' then Org_named=1;
if not missing(DEFENDANT_TYPE [g]) then n_defendant=n_defendant + 1;
end;
If you really have to define multiple arrays then the names you use for the arrays need to be different from each other and different from the names of any actual variables.
Or you can just use SAS functions to generate those two variables.
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
Org_named=0<whichc('Organization',of defendant_type(*));
n_defendant=dim(defendant_type) - cmiss(of defendant_type(*));
Show an example, including data in a datastep, and the result you want to achieve.
Art, CEO, AnalystFinder.com
There is no need to define a second array to scan the same list of variables a second time. In fact there is no need to scan over the array twice for that example. Just put both IF statements in the same DO loop.
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
Org_named=0;
n_defendant=0;
do g= 1 to 69;
if DEFENDANT_TYPE [g]='Organization' then Org_named=1;
if not missing(DEFENDANT_TYPE [g]) then n_defendant=n_defendant + 1;
end;
If you really have to define multiple arrays then the names you use for the arrays need to be different from each other and different from the names of any actual variables.
Or you can just use SAS functions to generate those two variables.
array DEFENDANT_TYPE [69] $ DEFENDANT_TYPE1-DEFENDANT_TYPE69;
Org_named=0<whichc('Organization',of defendant_type(*));
n_defendant=dim(defendant_type) - cmiss(of defendant_type(*));
That's very helpful, thanks - so if 2 arrays in different data steps reference the same columns Def1-Def69, can you use the same name or does the array name have to be different, even between data steps?
Thanks
Laura
@lmyers2 wrote:
That's very helpful, thanks - so if 2 arrays in different data steps reference the same columns Def1-Def69, can you use the same name or does the array name have to be different, even between data steps?
Thanks
Laura
An ARRAY is just a way to make it easier to reference a group of variables. It has no permanent definition. So you can use the same array name in multiple data steps. Of course the same name might not work in a different data step because it might conflict with the variable names that are being used in that data step. The main reason to use the same name for an array across multiple data steps is to make it easier for the human being reading the code.
You will need to show some test data in the form of a datastep following this post if needed:
You are getting those four errors because, as far as we can tell, the variables and arrays mentioned are never defined, at least in the code snippet you show, hence why it is always a good idea to post test data and full code.
This has been pointed out to you at least once before.
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.