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

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

data admit2;
set vnt_admit;
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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

15 REPLIES 15
Astounding
PROC Star

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.

robertrao
Quartz | Level 8

Great Thanks....

robertrao
Quartz | Level 8

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

data admit2;

set vnt_admit;

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;

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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

Astounding
PROC Star

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;

robertrao
Quartz | Level 8

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

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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???

data admit2;

set vnt_admit;

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

Astounding
PROC Star

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

robertrao
Quartz | Level 8


Thanks once again for your time.

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

robertrao
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 15 replies
  • 1416 views
  • 3 likes
  • 3 in conversation