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
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;
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;
@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);
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.