Help using Base SAS procedures

Using an ARRAY

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

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: 19,771

Re: Using an ARRAY

Posted in reply to chris21baseball_hotmail_com

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;

View solution in original post


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

Re: Using an ARRAY

Posted in reply to chris21baseball_hotmail_com

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: 7,468

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

Posted in reply to chris21baseball_hotmail_com

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.

Need further help from the community? Please ask a new question.

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