BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

I have the following:

 

data have;
input num x1 x2 x3;
datalines;
1 . . .
2 . . .
3 . . .
;
run;

 

suppose i want to make a multiplication table (pseudo code)

data want;

set have;

do i=1 to 3

  x&i = number*i;

end

run;

 

Basically, the variables x1-x3 contain in them information, namely, the multiplication factor, so I want using a loop to "assemble" the variable name.

 

Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
data have;
input num x1 x2 x3;
datalines;
1 . . .
2 . . .
3 . . . 
;
run;
data want;
set have;
array x{*} $ x1-x3;
do i=1 to 3;
  x[i] = num*i;
  if i = 3 then output;
end;
run;

View solution in original post

2 REPLIES 2
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
data have;
input num x1 x2 x3;
datalines;
1 . . .
2 . . .
3 . . . 
;
run;
data want;
set have;
array x{*} $ x1-x3;
do i=1 to 3;
  x[i] = num*i;
  if i = 3 then output;
end;
run;
Tom
Super User Tom
Super User

@VDD Why did you include the OUTPUT statement?

 

Don't tell SAS that the array has character variables when they are already defined as numeric should cause an error.  Also note there is no need to tell SAS how many elements are in the array when you have listed the names.  It will just count them.

array x x1-x3;

and you don't have to count them either, use the DIM() function.

do i=1 to dim(x);

 

sas-innovate-white.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.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 927 views
  • 2 likes
  • 3 in conversation