Solved
New Contributor
Posts: 3

# Using an ARRAY

I am attempting to convert the following code to using an ARRAY(s) instead of individual entries.

My Current code with individual entries is as follows:

Data WGTCHG(Drop=name dob marital race)DEMO(Keep=name dob marital race);

Set Wgt2.weights2;

CHANGEV2=wgt2-wgt1;

CHANGEV3=wgt3-wgt2;

CHANGEV4=wgt4-wgt3;

CHANGEV5=wgt5-wgt4;

CHANGEV6=wgt6-wgt5;

CHANGEV7=wgt7-wgt6;

CHANGEV8=wgt8-wgt7;

CHANGEV9=wgt9-wgt8;

CHANGEV10=wgt10-wgt9;

CHNAGEV11=wgt11-wgt10;

RUN;

I have been unsuccessful finding an example in these forums, UCLA and Stanford's SAS pages so any help would be greatly appreciated!

Thanks!

Scouter

Accepted Solutions
Solution
‎10-08-2014 02:54 PM
Super User
Posts: 23,778

## Re: Using an ARRAY

I'm assuming you have more code in your data step because you indicated two output data sets, but hopefully this helps:

Data  WGTCHG(Drop=name dob marital race) DEMO(Keep=name dob marital race);

Set Wgt2.weights2;

array change(2:11) changev2-changev11;

array wgt(1:11) wgt1-wgt11;

do i= 2 to 11;

change(i)=wgt(i)-wgt(i-1);

end;

RUN;

All Replies
Solution
‎10-08-2014 02:54 PM
Super User
Posts: 23,778

## Re: Using an ARRAY

I'm assuming you have more code in your data step because you indicated two output data sets, but hopefully this helps:

Data  WGTCHG(Drop=name dob marital race) DEMO(Keep=name dob marital race);

Set Wgt2.weights2;

array change(2:11) changev2-changev11;

array wgt(1:11) wgt1-wgt11;

do i= 2 to 11;

change(i)=wgt(i)-wgt(i-1);

end;

RUN;

PROC Star
Posts: 8,169

## Re: Using an ARRAY

I had the same suggestion as Fareeza, but suggest using _n_ rather than i, or adding i do your drop variables. _n_ will automatically be dropped.  i.e.:

do _n_= 2 to 11;

change(_n_)=wgt(_n_)-wgt(_n_-1);

New Contributor
Posts: 3

## Re: Using an ARRAY

Thank you so much! I've been at it for the past 3 hours and was about ready to pull my hair out!

🔒 This topic is solved and locked.

Discussion stats
• 3 replies
• 295 views
• 3 likes
• 3 in conversation