BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kngu022
Obsidian | Level 7
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? 
 Screen Shot 2020-02-16 at 4.20.32 PM.png

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @kngu022 

If you execute the code step by step:

 

  • In a dataset named 'work' (also in the library named work ...) you initialize an array, which is a group of variables: the array name is a, it contains numeric variables x1, x2, x3, x4 and x5. As 5 variables have been referenced, the 'dimension' of the array is 5. You can now call 'x1' 'a(1)' and 'x2' 'a(2)', ... which is very practical in do loops. As these variables are new, the value is missing.
ARRAY a(*) x1-x5;

Capture d’écran 2020-02-16 à 10.38.22.png

 

 

  • Then you use a do loop to compute the values of x1, x2, ... x5. The loop iterate from 1 to 5, which is the dimension of the array:
    • i=1 -> a(1), which is x1 is equal to 1**2 so 1
    • i=2 -> a(2), which is x2 is equal to 2**2 so 4
    • ...
    • At the end i = 6
DO i=1 to DIM(a);
	a(i)=i**2;
END;

Capture d’écran 2020-02-16 à 10.38.38.png

 

  • Then you compute the sum of variables x1 minus x3 -> 1  - 9 = -8. This is completely different of the sum of variables x1, x2 and x3 -> 1 + 4 + 9 = 14. The keyword of is very important here.
s1=sum(x1-x3);
s2=SUM(of x1-x3);

Capture d’écran 2020-02-16 à 10.39.20.png

 

 

 

 

  • And then, you put in the variable 'n' the iteration number. Here, it is the first observation, so _n_ = 1.

 

Capture d’écran 2020-02-16 à 10.39.35.png

n=_N_;

 

Finally, you keep only the following variables in the data step (KEEP=i s1 s2 n) : i s1 s2 n

 

Capture d’écran 2020-02-16 à 10.39.57.png

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

What do think it is doing?  What does each statement do?  There are only 9, counting the RUN.

Kurt_Bremser
Super User

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.

ed_sas_member
Meteorite | Level 14

Hi @kngu022 

If you execute the code step by step:

 

  • In a dataset named 'work' (also in the library named work ...) you initialize an array, which is a group of variables: the array name is a, it contains numeric variables x1, x2, x3, x4 and x5. As 5 variables have been referenced, the 'dimension' of the array is 5. You can now call 'x1' 'a(1)' and 'x2' 'a(2)', ... which is very practical in do loops. As these variables are new, the value is missing.
ARRAY a(*) x1-x5;

Capture d’écran 2020-02-16 à 10.38.22.png

 

 

  • Then you use a do loop to compute the values of x1, x2, ... x5. The loop iterate from 1 to 5, which is the dimension of the array:
    • i=1 -> a(1), which is x1 is equal to 1**2 so 1
    • i=2 -> a(2), which is x2 is equal to 2**2 so 4
    • ...
    • At the end i = 6
DO i=1 to DIM(a);
	a(i)=i**2;
END;

Capture d’écran 2020-02-16 à 10.38.38.png

 

  • Then you compute the sum of variables x1 minus x3 -> 1  - 9 = -8. This is completely different of the sum of variables x1, x2 and x3 -> 1 + 4 + 9 = 14. The keyword of is very important here.
s1=sum(x1-x3);
s2=SUM(of x1-x3);

Capture d’écran 2020-02-16 à 10.39.20.png

 

 

 

 

  • And then, you put in the variable 'n' the iteration number. Here, it is the first observation, so _n_ = 1.

 

Capture d’écran 2020-02-16 à 10.39.35.png

n=_N_;

 

Finally, you keep only the following variables in the data step (KEEP=i s1 s2 n) : i s1 s2 n

 

Capture d’écran 2020-02-16 à 10.39.57.png

kngu022
Obsidian | Level 7

@ed_sas_member Thanks very much. Your direction is so so clear and easy to follow up. 

ed_sas_member
Meteorite | Level 14

Hi @kngu022 

You're welcome!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 1064 views
  • 3 likes
  • 4 in conversation