## Arrays

Solved
Super Contributor
Posts: 1,041

# Arrays

In the same dataset I am trying to acheive two things;

1)to extract the last item in the UNITS array

2)to do the difference between time pairs(Datetime 16. format) and sum them up..

I am not getting the days right....Days are shown as 10008.3465 etc etc Could you help me correct it

array units unext10-unext1;
array itb int:;
array ext ext:;
do over units;
if not missing (units) then do;
new=units;LEAVE;
end;
END;
do i=1 to dim(itb);
if 2=N(int(i),ext(i)) then duration=sum(duration,ext(i)-int(i));
days=duration/86400;
end;
run;

EVEN I DO IT AS SEPERATE DATASETS I DONT GET IT .......

Thanks

Accepted Solutions
Solution
‎05-28-2013 05:08 PM
Super User
Posts: 6,785

## Re: Arrays

Once you define this array:

array itb int:;

You have to refer to the array as ITB.  You can't refer to the array as INT.  INT(i) is applying the INTEGER function to i, not referring to the array at all.

So anywhere you have int(i), replace it with itb(i).

Good luck.

All Replies
Solution
‎05-28-2013 05:08 PM
Super User
Posts: 6,785

## Re: Arrays

Once you define this array:

array itb int:;

You have to refer to the array as ITB.  You can't refer to the array as INT.  INT(i) is applying the INTEGER function to i, not referring to the array at all.

So anywhere you have int(i), replace it with itb(i).

Good luck.

Super Contributor
Posts: 1,041

## Re: Arrays

Great Thanks....

Super Contributor
Posts: 1,041

## Re: Arrays

Hi ,

Is there anyway i can do the same operation using a single do loop in the below code????

1)to extract the last item in the UNITS array

2)to do the difference between time pairs(Datetime 16. format) and sum them up..

array units unext10-unext1;

array itb int:;

array ext ext:;

do over units;

if not missing(units) then do;

new=units;LEAVE;

end;

END;

do i=1 to dim(itb);

if 2=N(itb(i),ext(i)) then duration=sum(duration,ext(i)-itb(i));

end;

days=duration/86400;

run;

Super User
Posts: 6,785

## Re: Arrays

Possibly, but it would only make the program more complex and difficult to follow.  Is there some reason you need to do this?  If so, a related question would be whether you know how many INT and EXT values you have.

Super Contributor
Posts: 1,041

## Re: Arrays

Hi,

I was thinking of different ways since i am very naive to using arrays

secondly, yes in this case i know the value for INT AND EXT  its 10 .

but all 10 need not necessarily have values upto 10......some might have values only for 4 pairs and the rest pairs are blank

Some might have values all the way thru 10

Thanks

Super User
Posts: 6,785

## Re: Arrays

OK, having 10 elements in each array simplifies things.  You could try:

do i=1 to 10;

if missing(new) then new=units{i};

if 2=N(itb{i}, ext{i}) then duration = sum(duration, ext{i} - itb{i});

end;

Super Contributor
Posts: 1,041

## Re: Arrays

I think you have mistaken

if missing(new) then new=units{i};

FOR

if not missing(units) then new=units{i}; because we need the non missing value

Also, even if we do this  below

if not missing(units) then new=units{i}; we are not still making sure that it is grabbing the last non missing!!! we want the last non missing

Thanks

Super User
Posts: 6,785

## Re: Arrays

No, the logic is just the way I intended it.

The program keeps placing the units{i} elements into NEW.  Once NEW has a value, the remaiing units{i} values can be ignored.

Super Contributor
Posts: 1,041

## Re: Arrays

i think i dint get it still...Could you please explain

How can we use the variable NEW before we even created it???

and which step in the code makes sure that it picks the last non missing value from the array UNITS??

Thanks

Super User
Posts: 6,785

## Re: Arrays

Q1.  The DATA step operates in two phases.  First, it examines your statements, checks syntax, and sets up storage space for all your variables.  This is generally called the compilation phase of the data step.  Once all this set-up work is done, NEW exists.  Only then does the DATA step enter the second phase and actually execute your statements.  If this is something you want to study up on, you can probably find two related papers (paraphrasing the titles here):  The SAS Supervisor (Don Henderson), and How MERGE Really Works (Robert Virgile).

Q2.  The list of variables in the UNITS array is UNEXT10-UNEXT1.  So the first time through the DO loop, when i=1, UNEXT10 gets copied to NEW.  At this point, NEW may be populated or it may be missing ... it depends on what was in UNEXT10.  The second through the DO loop, when i=2, the loop examines whether NEW is missing or not.  If it's missing, UNEXT9 gets copied into NEW.  So once NEW has a value, any remaining elements within the UNITS array will not get placed into NEW.

Super Contributor
Posts: 1,041

## Re: Arrays

Thanks for the detailed explanation.

Reading very closely, i think i=2 in the below menioned quote...Am i correct???

"The second through the DO loop, when i=1, the loop examines whether NEW is missing or not."

2)And after all the modifications doesnt the final code look like this???

array units unext10-unext1;

array itb int:;

array ext ext:;

do i=1 to 10;

if missing(new) then new=units{i};

if 2=N(itb{i}, ext{i}) then duration = sum(duration, ext{i} - itb{i});

end;

days=duration/86400;

run;

Thanks

Super User
Posts: 6,785

## Re: Arrays

Correct on all counts.  I fixed i=1, making it i=2 for anybody else reading it.

Super Contributor
Posts: 1,041

## Re: Arrays

Thanks once again for your time.

This is a very nice place to learn and share the subject

Super Contributor
Posts: 1,041

## Re: Arrays

In the below can i use any name for the variable being created?????

if 2=N(itb{i}, ext{i}) then duration = sum(duration, ext{i} - itb{i});

Example:

if 2=N(itb{i}, ext{i}) then Time = sum(duration, ext{i} - itb{i});

Can anyone explain the logic??

Thanks

🔒 This topic is solved and locked.

Discussion stats
• 15 replies
• 469 views
• 3 likes
• 3 in conversation