Help using Base SAS procedures

ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Reply
Regular Contributor
Posts: 157

ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Hi,

I am having a problem with arrays when there are no records in the dataset

PROC SQL NOPRINT;

SELECT COUNT(*) INTO :CNT

FROM DETAILS;

QUIT;

DATA TEST;

SET PERSONS;

IF &CNT > 0 THEN DO;

ARRAY DTL {&CNT} $10. _TEMPORARY_;

DO i=1 to &CNT;

DTL{i}=DETAIL;

OUTPUT;

END;

END;

RUN;

I kept the condiiton to do it only in cases where the record count is > 0 but when i ran this program and there is no data it throws the error

ERROR: Invalid dimension specification for array DTL.  The upper bound of an array dimension is smaller than its corresponding

       lower bound.

Why it is generating this error even when i kept the condition &CNT > 0 .

Super User
Super User
Posts: 7,060

Re: ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Array statements are not executable. They are just used to setup the data step.  So it is the same as if the ARRAY statement is outside of the IF/THEN block.  Same for other non-executable statements like KEEP/DROP/RENAME/ etc.

Your example program does not make any sense.  You are assigning values to temporary variables that will just be thrown away.

What do you actually want to do? Perhaps there is a better way to do it?

Regular Contributor
Posts: 157

Re: ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Hi Tom,

I am doing a cartesian join there.

For example i have a dataset like this Persons

Persons

John

Tom

Jim

Drug Tests

TB

BB

CF

i am creating a data set like this

John  TB

John BB

John CF

JIm TB

Jim BB

JIm CF

Super User
Posts: 19,822

Re: ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Cross join in SQL instead?

proc sql;

create table want as

select p.*, t.*

from persons p

cross join

tests t;

quit;

Super User
Super User
Posts: 7,060

Re: ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

SQL is the easiest.

To do it with a data step you do not need any macro variables.

data want ;

  set persons;

  do i=1 to nobs;

     set drugtests nobs=nobs point=i;

     output;

  end;

run;


If you want to keep the people when there are no drug tests you can add.


if nobs=0 then output;


Trusted Advisor
Posts: 1,022

Re: ARRAY PROBLEMS WHEN THERE ARE NO RECORDS

Tom has pointed out the erroneous assumptions your program embodies about the non-executable nature of the ARRAY statement, and even if the program ran to completion it wouldn't do anything.

But assuming the program did do some real work, (say you were looking for the 2nd largest value in a collection), you could still use &CNT as an upper bound if you used 0 as a lower bound, as in

  ARRAY DTL {0:&CNT} $10. _TEMPORARY_;

The array statement is still not executable, but if &CNT is zero, this statement would have avoided the implicit definition of an array with lower bound of 1 (the default) and upper bound of 0.  That's what provoked the "upper bound of an array is smaller than ..." message.

Ask a Question
Discussion stats
  • 5 replies
  • 408 views
  • 0 likes
  • 4 in conversation