DATA work(KEEP = i s1 s2 n); ARRAY a(*) x1-x5; DO i = 1 to DIM(a); a(i) = i**2; END; s1 = sum(x1-x3); s2 = SUM(of x1-x3); n = _N_; RUN; PROC PRINT DATA=work; RUN;
I have the code above. When I. put it in SAS and the result comes out like in the picture. Can anyone help me to understand this code and how it works please?
Thanks
Hi @kngu022
If you execute the code step by step:
ARRAY a(*) x1-x5;
DO i=1 to DIM(a);
a(i)=i**2;
END;
s1=sum(x1-x3);
s2=SUM(of x1-x3);
n=_N_;
Finally, you keep only the following variables in the data step (KEEP=i s1 s2 n) : i s1 s2 n
What do think it is doing? What does each statement do? There are only 9, counting the RUN.
The code illustrates the difference of using x1-x3 as a formula (in the first sum() call) or as a list of variables (in the second sum() call) when the prefix "of" is used.
Hi @kngu022
If you execute the code step by step:
ARRAY a(*) x1-x5;
DO i=1 to DIM(a);
a(i)=i**2;
END;
s1=sum(x1-x3);
s2=SUM(of x1-x3);
n=_N_;
Finally, you keep only the following variables in the data step (KEEP=i s1 s2 n) : i s1 s2 n
@ed_sas_member Thanks very much. Your direction is so so clear and easy to follow up.
Hi @kngu022
You're welcome!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.