DATA Step, Macro, Functions and more

array getting updated but not data-set

Reply
N/A
Posts: 0

array getting updated but not data-set

Hi Guys,
I am trying to update a column of a table, the values of the column are loaded into an array.
But what i observe is that the array is getting updated but no the data-set, please let me know what i am doing wrong.

data test;
input id;
datalines;
0
5
1
-6
-1
4
1
-7
-9
4
run;
data test1 (keep=id2);
set test nobs=nobs end=eof;
array a_id1(100) _temporary_;
array a_id2(100) _temporary_;
a_id1(_n_) = id;
a_id2(_n_) = id2;
if eof then
do i = 1 to nobs;
a_id2(i) = a_id1(i)*100;
output test1;
end;
run;
proc print data=test1;
run;

Regards
Sanjeev
Super Contributor
Super Contributor
Posts: 3,174

Re: array getting updated but not data-set

So, what SAS code assigns a new value to ID2 in WORK.TEST (that is the only variable you have in your KEEP= list)?

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: array getting updated but not data-set

Hi Scott,
I have loaded the id2 in a_id2 using the following statement.
a_id2(_n_) = id2;

Using the above statement i suppose we have linked id2 with the array a_id2 and whenever i change something in the array it should reflect in the data set.

I am new to SAS, please forgive me for my ignorance.

Sanjeev
SAS Super FREQ
Posts: 8,743

Re: array getting updated but not data-set

Hi:
You stated this assumption/desire:
whenever i change something in the array it should reflect in the data set

However, your array definitions are _temporary_ and if you consult the documentation, you will find in this example:
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000739642.htm
the explanation that:
"Although they behave like variables, temporary array elements do not have names, and they do not appear in the output data set." Full documentation on _TEMPORARY_ is here:
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000201956.htm

As shown in this example:
http://support.sas.com/kb/24/773.html
if you want to "touch" the permanent dataset variables, you must use what is in the temporary array to explicitly change the values of the dataset variables you want to change/keep.

This is a good introductory paper on using ARRAYs with SAS (although, I find the documentation to be quite thorough and comprehensive, as well).
http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

Studying a bit more on arrays and how they work in SAS should help you come to a resolution of your issue.

cynthia
N/A
Posts: 0

Re: array getting updated but not data-set

Thanks Cynthia for the references, The problem with my requirement is that i need to change the values of some other row and not the row for which iteration is happening.
Valued Guide
Posts: 632

Re: array getting updated but not data-set

If I understand correctly you really do not need the second array. You are loading the first with original values one obs at a time. Then using those values one at a time. The new step becomes:
[pre]
data test1 (keep=id2);
set test nobs=nobs end=eof;
array a_id1(100) _temporary_;
a_id1(_n_) = id;
if eof then
do i = 1 to nobs;
id2 = a_id1(i)*100;
output test1;
end;
run;[/pre]
of course if this is truly not just a very simplified version of what you want, no array is needed at all.
[pre]
data test1 (keep=id2);
set test;
id2 = id*100;
run;[/pre]
Ask a Question
Discussion stats
  • 5 replies
  • 134 views
  • 0 likes
  • 4 in conversation