Hello, my friends:
I have a dataset one including 103 variables of a, b, c, d1, d2, d3, ..., d100. I want to creat a new dataset two also including these 103 variables, but the requiremen should:
1) keep orginal variables of a, b, c, and their original values;
2) keep all variables of d1, d2, d3, ....d100, but their values should be their orginal values divided by the value of c variable.
I don't want to do following code:
data one;
set two;
d1=d1/c;
d2=d2/c;
.......
d100=d100/c
proc print;
run;
Thanks a lot in advance!!
It's time to learn about arrays. This paper should provide all you need: Arrays Made Easy: An Introduction to Arrays and Array Processing
In my case, d1, d2, d3,...d100, actually represent different name of varibles. I am not sure if array method could work it out!
You will still need to learn arrays. Here's a hint on how to approach the problem:
data want;
length a b c 8;
set have;
array nums {*} _numeric_;
Because of the LENGTH statement, you are guaranteed that A, B, and C are the first three elements of the NUMS array.
This does assume that the LENGTH statement is valid, meaning A, B, and C are all numeric variables. If A or B might be character, you still need to use arrays. But it becomes more complex because of the need to use the VNAME function.
In addition to learning about arrays, you need to learn about abbreviated variable lists, as in
data have;
input a b c d e f g h I j k;
datelines;
101 102 103 104 105 106 107 108 109 110 111
201 202 203 204 205 206 207 208 209 210 211
301 302 303 304 305 306 307 308 309 310 311
401 402 403 404 405 406 407 408 409 410 411
501 502 503 504 505 506 507 508 509 510 511
run;
data want;
set have;
array x {*} d--k;
....
run;
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.
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.