BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lmyers2
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

Tom
Super User Tom
Super User

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

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

Tom
Super User Tom
Super User

@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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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