SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to create a new dataset by keeping some variables and updating values of other variables?

Reply
New Contributor YHJ
New Contributor
Posts: 4

How to create a new dataset by keeping some variables and updating values of other variables?

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

Trusted Advisor
Posts: 1,312

Re: How to create a new dataset by keeping some variables and updating values of other variables?

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

New Contributor YHJ
New Contributor
Posts: 4

Re: How to create a new dataset by keeping some variables and updating values of other variables?

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

Super User
Posts: 6,648

Re: How to create a new dataset by keeping some variables and updating values of other variables?

[ Edited ]

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.

Trusted Advisor
Posts: 1,312

Re: How to create a new dataset by keeping some variables and updating values of other variables?

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;

 

Ask a Question
Discussion stats
  • 4 replies
  • 162 views
  • 0 likes
  • 3 in conversation