BookmarkSubscribeRSS Feed
Mando
Calcite | Level 5

I am new to SPSS and I am trying to figure out how mix a do-loop with an array to do the following:

write a SAS code using SAS data step only and “do loop” so that you can modify the above data set to get a new data set having 21 variables (X1-X21) which will look like below:

 

X1       X2       X3           …                X21    

 1          .           .                                   .          

 2          1          .                                   .

 3          2          1                                  .

 21        19        18                                1

 

 

The data set used is the following:

 

data example;
input X;
datalines;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;
RUN;

 

I am trying to figure out the shortest way to get the proper output and if someone could help me out and give me some tips I would be very greatful.

2 REPLIES 2
maguiremq
SAS Super FREQ

There are users on here that are much, much better at DO loops compared to me. This is my best shot, and I'm certain there is a better method.

 

data example;
input X;
datalines;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;
RUN;

data want;
	set example;
	array _x [*] 3. x1 - x21;
	do i = 1 to dim(_x);
		if i = 1 then do;
			_x[i] = x;
		end;
		else do;
			_x[i] = lag(_x[i-1]);
		end;
	end;
run;

I think there might be a typo in your 'want' data set, or I did something wrong.

 

X x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 
1 1 . . . . . . . . . . . . . . . . . . . . 
2 2 1 . . . . . . . . . . . . . . . . . . . 
3 3 2 1 . . . . . . . . . . . . . . . . . . 
4 4 3 2 1 . . . . . . . . . . . . . . . . . 
5 5 4 3 2 1 . . . . . . . . . . . . . . . . 
6 6 5 4 3 2 1 . . . . . . . . . . . . . . . 
7 7 6 5 4 3 2 1 . . . . . . . . . . . . . . 
8 8 7 6 5 4 3 2 1 . . . . . . . . . . . . . 
9 9 8 7 6 5 4 3 2 1 . . . . . . . . . . . . 
10 10 9 8 7 6 5 4 3 2 1 . . . . . . . . . . . 
11 11 10 9 8 7 6 5 4 3 2 1 . . . . . . . . . . 
12 12 11 10 9 8 7 6 5 4 3 2 1 . . . . . . . . . 
13 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . . . . . 
14 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . . . . 
15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . . . 
16 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . . 
17 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . 
18 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . 
19 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . 
20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . 

 Wanted to add that there are better ways to do this. If you're being forced to use a DO loop for it, I'd assume it's an assignment.

 

Never used it personally, but I think (after seeing other posts suggesting it) that PROC EXPAND would be a good fit here.

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/etsug/etsug_expand_syntax02.htm

 

 

PaigeMiller
Diamond | Level 26

What is being subtracted here (from column to column)?

 

Is this is a more general problem where the data can be any numbers, or does it apply only to data which is consecutive integers (in which case no subtraction is needed)?

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1203 views
  • 0 likes
  • 3 in conversation