DATA Step, Macro, Functions and more

help with array or macro, please?

Reply
Contributor
Posts: 26

help with array or macro, please?

Hello,

Happy 4th of July to all of you!

 

This is a simple question that I used to be able to do, but can't, for the life of me, figure out.  I have attached a document in WORD on how the data is displayed (it's wide longitudinal or multivariate form). I can easily transpose it to the long form. 

 

This is what I want to do, but there has to be a simpler way. I can do this little bit now, but I will be getting more data with repeated scorings. 

 

1. Attachment = wide form of data = the data I'm interested in is s1-s20.  I am taking the difference between them at each level. I already did the proc mixed, etc. but how could I write the code (in #2) easier? 

 

2.  Here is what I need do:

 

data want;

set have;

      d1=s2-s1;

            d2=s3-s2;

            d3=s4-s3;

            d4=s5-s4;

 

            d5=s6-s5;

            d6=s7-s6;

            d7=s8-s7;

            d8=s9-s8;

 

 

            d9=s10-s9;

            d10=s11-s10;

            d11=s12-s11;

            d12=s13-s12;

 

            d13=s14-s13;

            d14=s15-s14;

            d15=s16-s15;

            d16=s17-s16;

 

            d17=s18-s17;

            d18=s19-s18;

            d19=s20-s19;

 

            run;

 

 

How can I do this easier?   I thank you for your time. Marysia.

Trusted Advisor
Posts: 1,256

Re: help with array or macro, please?

Hello Marysia,

 

Good idea to use arrays here (no need for macros). Indeed, this is a typical use case for arrays.

 

data want;
set have;
array s[20]; /* handle existing variables s1, ..., s20 as array elements s[1], ..., s[20] */
array d[19]; /* create new variables d1, ..., d19 and handle them as array elem. d[1], etc. */
do i=1 to 19;
  d[i]=s[i+1]-s[i]; /* array indices allow to compute the differences in a loop */
end;
drop i; /* index variable is no longer needed */
run;
Ask a Question
Discussion stats
  • 1 reply
  • 58 views
  • 0 likes
  • 2 in conversation