BookmarkSubscribeRSS Feed
Irene999
Calcite | Level 5

Hi, I am new here... just need to understand some SAS codes like below... I was told these are arrays... but SAS array documents use square bracket instead of curly bracket after arrays... then what are they?

 

http://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf

 

What is array? Are they independent from table`s fields? 

 

data xxx3;
 set xxx2;

 %let d=&dim;

 array output {&d};
 array input {&d};
 array pppdate {&d};
 array ind {&d};
 array ccc {&d};
 array ddd {&d};
 
    
 do i=0 to (&d-1);

  if ccc{&d-i}=1 then do;
   if pppdate{&d-i} ne . then do;
    naec=0;
    if daaate<=pppdate{&d-i} then taec=pppdate{&d-i}-daaate;
   end;
   else do;
    naec=1;
    if daaate<=input{&d-i} and input{&d-i} ne . then taec=input{&d-i}-daaate;
   end;
  end;

  

       

end;


do a=1 to &d;

      if input{a}>daaate and ind{a}=0 and
         (input{a}-outdate>3 or fstEMGC=1 or (ddate^=. and ddate=output{a}-input{a}+1)) then do;
         if ddd{a}=1 and STRREC=. then STRREC=input{a}-daaate;
      end;

run;

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can use [] or {} it doesn't matter, just be consistent in which ones you use.  Some prefer [] as it reflect other languages more, some prefer {}.  Its mainly to separate them out from function calls which use (), so I would never advise using those.

 

An array is different to other languages.  An array in SAS is a reference to variables in the dataset, either ones which exist or ones created by the array statement, but the array itself is nothing more than an alias for a list of variables in a dataset, it is not a data container itself.

 

I would suggest looking at some coding standards.

This:

data xxx3;
 set xxx2;

 %let d=&dim;

Is not good coding, call datasets at least reasonable names.  Also, don't put %let statements in datastep code, especially when they don't actually do anything as:

data xxx3;
 set xxx2;

 array output {&dim.};
 array input {&dim.};

Is the same, you just create one extra macro variable for no reason.  

Always finish macro variables with a point as I have done above, in 70% of the time it doesn't matter, but sometimes it does, and its good practice, it should also highlight in your code window better.

Generally speaking if you have lots of the same variables, as indicated by loads of array statements, a bit of data remodeling will make your life easier, so from (just guessing what your data looks like):
output1 output2 output3 input1 input2 input3 ...ddd1 ddd2 ddd3

1           3           2            8        9         1           12      14     13

...

 

To

Order   Output  Input ... ddd

1           1          8           12

2           3          9            14

3           2          1            13

 

In this way you code simplifies down to something like this:

data xxx3;
 set xxx2;
 if ccc=1 then do;
   if pppdate ne . then do;
     naec=0;
      if daaate<=pppdatethen taec=pppdate-daaate;
   end;
   else do;
     naec=1;
     if daaate<=... ne . then taec=...-daaate;
   end;
  end;
  do a=1 to &dim.;
    if a > daaate and ind=0 and
         (a-outdate > 3 or fstEMGC=1 or (ddate ne . and ddate=output-a+1)) then do;
      if ddd=1 and STRREC=. then STRREC=a-daaate;
    end;
  end;
run;

Note, I have no test data in the form of a datastep or required output to work with, so can't really tell what the have and want are to provided acurate code.

 

Irene999
Calcite | Level 5

Hi, could you explain a bit more of what the do loop is doing? for me, it just change or keep the value of naec which just contains one number.. 

 

Yes, I have lots of arrays like ccc, (not variables),  all going through the same process, but I dont understand what you mean by remodeling them... can you explain a bit more? TIA

RW9
Diamond | Level 26 RW9
Diamond | Level 26

A do loop iterates the code between the do and end the number of times indicated by the do statement.  So in this instance:

do a=1 to &dim.;

the code up to the corresponding end statement will be executed &dim. times, with the variable a being incremented by 1 on each iteration.

 

Modelling data is the technique of efficiently storing and using data.  There are manya ways to store data, some are faster to use but take more space, some are easier for different ways of thinking e.g. Normalised versus Transposed.  In this instance you have all the data going across the page, it is called transposed and is often associated with Excel working.  Normalised, means having a small set of variables where the data go down the page rather than across - mostly used in database and such like.  There are benefits to both, although my preference leans towards the latter.  I provided an example earlier of each:

 

Transposed (as you have it now)

output1 output2 output3 input1 input2 input3 ...ddd1 ddd2 ddd3

1           3           2            8        9         1           12      14     13

...

 

Nomalised

Order   Output  Input ... ddd

1           1          8           12

2           3          9            14

3           2          1            13

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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