BookmarkSubscribeRSS Feed
mgrzyb
Quartz | Level 8

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.

1 REPLY 1
FreelanceReinh
Jade | Level 19

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;

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
  • 1 reply
  • 540 views
  • 0 likes
  • 2 in conversation