DATA Step, Macro, Functions and more

question about naming arrays differently within a data step

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

question about naming arrays differently within a data step

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?


Accepted Solutions
Solution
‎01-22-2018 01:44 PM
Super User
Super User
Posts: 7,938

Re: question about naming arrays differently within a data step

[ Edited ]

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(*));

View solution in original post


All Replies
PROC Star
Posts: 8,149

Re: question about naming arrays differently within a data step

Show an example, including data in a datastep, and the result you want to achieve.

 

Art, CEO, AnalystFinder.com

 

Solution
‎01-22-2018 01:44 PM
Super User
Super User
Posts: 7,938

Re: question about naming arrays differently within a data step

[ Edited ]

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(*));
Contributor
Posts: 44

Re: question about naming arrays differently within a data step

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

Super User
Super User
Posts: 7,938

Re: question about naming arrays differently within a data step

[ Edited ]

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.

Super User
Super User
Posts: 9,427

Re: question about naming arrays differently within a data step

You will need to show some test data in the form of a datastep following this post if needed:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 124 views
  • 0 likes
  • 4 in conversation