BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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.
ArtC
Rhodochrosite | Level 12
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]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 744 views
  • 0 likes
  • 4 in conversation