DATA Step, Macro, Functions and more

Question about using Array and Retain in Data Step...

Reply
N/A
Posts: 0

Question about using Array and Retain in Data Step...

I will appreciate if someone can explain what the following code is doing. I do not have a way to debug this.

Thanks in advance.

Code:

data bk_baddata2 (KEEP = loan_id newbaddata4 newbaddata6 newbaddata7);
set bk_baddata;
array bad(*) baddata4 baddata6 baddata7;
array new(*) newbaddata4 newbaddata6 newbaddata7;
retain newbaddata4 newbaddata6 newbaddata7;
by loan_id;
if first.loan_id then
do;
newbaddata4 = .;
newbaddata6 = .;
newbaddata7 = .;
end;
do i = 1 to 3;
if bad(i) NE . then new(i) = bad(i);
end;
if last.loan_id;
run;
Super Contributor
Posts: 326

Re: Question about using Array and Retain in Data Step...

Posted in reply to deleted_user
Hi,

the code actually sets the value of the baddata columns equal to the newbaddata columns if the baddata does not have a null value. then the last record per loan_id is only written into the dataset bk_baddata2 along with the loan_id and newbaddata columns.

regarding the use of retain, i'm not quite sure but i think the retain part and the if first.loan_id part is redundant. no default values were set in the retain statement so the default value will be missing. i'm not sure if both are really needed.

by any chance do you know what the result table should look like?

hope this helps. if ever i explained something wrong please do correct it. thanks!

Milton
N/A
Posts: 0

Re: Question about using Array and Retain in Data Step...

Thanks Milton, appreciate your help.

Unfortunately I do not have the result Table to match with.
N/A
Posts: 0

Re: Question about using Array and Retain in Data Step...

Posted in reply to deleted_user
One question still remains, do the following three blocks execute for every observation ?

Specially, the #2 DO loop. Does it execute for each observation?

(1) if first.loan_id then
do;
newbaddata4 = .;
newbaddata6 = .;
newbaddata7 = .;
end;

(2) do i = 1 to 3;
if bad(i) NE . then new(i) = bad(i);
end;

(3) if last.loan_id;
Super Contributor
Super Contributor
Posts: 3,174

Re: Question about using Array and Retain in Data Step...

Posted in reply to deleted_user
It all depends on your input data - if you only have one obs per LOAN_ID, the answer is yes, otherwise #1 and #3 is NO, #2 is NO (it executes 3 times per observations, given your DO criteria.

And, why would you be asking the questions (other than a homework/quiz assignment), if you actually don't have any data to work through the discovery process with your own SAS system, input data, combined with some PUTLOG _ALL_; commands?

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 4 replies
  • 216 views
  • 0 likes
  • 3 in conversation