BookmarkSubscribeRSS Feed
YHJ
Calcite | Level 5 YHJ
Calcite | Level 5

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!!

4 REPLIES 4
mkeintz
PROC Star

It's time to learn about  arrays.  This paper should  provide all you need: Arrays Made Easy: An Introduction to Arrays and Array Processing

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
YHJ
Calcite | Level 5 YHJ
Calcite | Level 5

In my case, d1, d2, d3,...d100, actually represent different name of varibles. I am not sure if array method could work it out!

Astounding
PROC Star

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.

mkeintz
PROC Star

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 896 views
  • 0 likes
  • 3 in conversation