BookmarkSubscribeRSS Feed
DaveStar
Obsidian | Level 7

Hello

Let's say that in the data set I have there are 13 variables called : ID ,X0-X11

Let's say that the task is to create a series of new 11 variables called W1-W11 that their values will be as following:

W1=(X1-X0)/X0;

W2=(X2-X1)/X1;

and so on

W11=(X11-X10)/X10;

 

What is the way to write it by defining Arrays?

How many one dimensional-Arrays should be defined?

What is the way to do it by defining one two-dimensional-Array?

I am trying to write the code. What do you think? Is it correct?

Please note that I defined two one-dimensional arrays but with different dim (For Array XXX the dim is 12 and for WWW the dim is 11)

Data Tbl2;

SET tbl1;

ARRAY  XXX(12)  X0-X11;

ARRAY WWW(11) W1-W11;

DO i=1  TO 11;

WWW{i}=(XXX{i+1}-XXX{i})/XXX{i};

end;

Run;

 

2 REPLIES 2
ballardw
Super User

Did you try running the code?

Did you get the expected result?

 

Any values of 0 in the X array? You might want to fix the division by zero.

 

I might suggest for limits so it is a bit more obvious which array size is controlling the limit. Also if later you have more or fewer variables in the array then you don't need to change the array limit.

DO i=1  TO dim(www);

 

Normally the only place I personally use the { } braces are in array definitions and then use [ ] when referencing array elements but that is mostly a style choice.

hashman
Ammonite | Level 13

@DaveStar:

Yes, you can use both two 1-dimensional arrays and one 2-dimensional array for the purpose.

However, in the latter case you'd need to add a dummy variable because the number of variables in every row and column must be identical. It stands to reason, as a 2-dim array is a rectangular matrix, and you cannot have it with a corner cell simply absent. Compare the second and third steps below:

data tb1 ;                                                                                                                              
 array xxx [12] x0-x11 (0 1 . 2 3 4 5 . 6 0 8 7) ;                                                                                      
run ;                                                                                                                                   
                                                                                                                                        
data tbl2 ;                                                                                                                             
  set tb1 ;                                                                                                                             
  array xxx [12] x0-x11 ;                                                                                                               
  array www [11] w1-w11 ;                                                                                                               
  do i = 1 to dim (xxx) - 1 ;                                                                                                           
    www[i]= divide (xxx[i+1] - xxx[i], xxx[i]) ;                                                                                        
  end ;                                                                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data tbl2 (drop = _:) ;                                                                                                                 
  set tb1 ;                                                                                                                             
  array xw [2, 12] x0-x11 w1-w11 _wdummy ;                                                                                              
  do i = 1 to dim (xw, 2) - 1 ;                                                                                                         
    xw[2,i]= divide (xw[1,i+1] - xw[1,i], xw[1,i]) ;                                                                                    
  end ;                                                                                                                                 
run ;          

Heed also what @ballardw has said. First, compared to simple square brackets, curly braces are pain both to type and look at. Personally, I always use square brackets both in array definitions and references. Second, the DIVIDE function is much preferable to the division operator in terms of auto-handling division by zero and by null. (If you run the above code, you'll see some "missing values generated" notes in the log but they result from the minus (-) operator.)

 

Kind regards

Paul D.    

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 550 views
  • 0 likes
  • 3 in conversation