Help using Base SAS procedures

Questions about Array

Reply
Super Contributor
Posts: 1,041

Questions about Array

*Question1;

if i have 10 different observations(subjects) and I am creating an array in that dataset like

j=1

L=1

do i =1 to 30;

date(i)=

Suppose if the 30 of the newly created variables have dates like 05/01/2013 05/02/2013.............05/30/2013

Will these be retained for all the 10 subjects as well????

Can some one show me with an example???

Regards

Super Contributor
Posts: 1,041

Re: Questions about Array

Posted in reply to robertrao

*Question2;

j=1

L=1

do i =1 to 30;

if variable x ne " " then do;

variable(j)=;                            /*j has lesser array length than i*/

j+1;

end;

run;

Why do the j=1 and L=1 have to be outside of the do loop??

if i put inside of the do loop only the last iteration is outputted!!!!

when do we have to use J+1 ?Could someone explain the logic please with an example preferably!!!!

Thanks

Valued Guide
Posts: 634

Re: Questions about Array

Posted in reply to robertrao

In the first step that follows the 30 dates are created for each observation so retaining is not necessary.

data want;

   set sashelp.class;

   array dates {30} date1-date30;

   do date = '01may2013'd to '30may2013'd;

      output want;

   end;

   run;

In the second example the array is filled once and then the observations are read in a DOW loop.  This step could be simplified, but will be more efficient for large data sets.  In either case I am not sure that you really want the thirty dates repeated for each observation.

data want2;

   retain date1-date30 .;

   array dates {30} date1-date30;

   i=0;

   do date = '01may2013'd to '30may2013'd;

      i+1;

      dates{i}=date;

   end;

   do until(done);

      set sashelp.class end=done;

      output want2;

   end;

   stop;

   run;

Super Contributor
Posts: 1,041

Re: Questions about Array

Sorry sir,

That was my mistake..

there was another step in between......which i added in the below code now....

According to my understanding the below creates dates1 to dates30 and if my macro variables

mo=JAN

yr=2011

dates(i)=mdy("01&mo.&yr."d),i,&yr);

is this part retaining the dates????

j=1

L=1

do i =1 to 30;

dates(i)=mdy("01&mo.&yr."d),i,&yr);

if variable x ne " " then do;

variable(j)=.........;                            /*j has lesser array length than i*/

j+1;

end;

run;

Valued Guide
Posts: 634

Re: Questions about Array

Posted in reply to robertrao

Even so this is not a full DATA step.  there is no DATA or SET statement.The macro variables are defined elsewhere?

Retaining values is across observations.  Since we cannot see where the observations are coming from or what variables are on the PDV, we have to guess as to what is being retained.

Can you perhaps modify one of my silly examples from above using the CLASS data set to show us what you have so far and what you want to have happen?

Valued Guide
Posts: 634

Re: Questions about Array

Posted in reply to robertrao

Array values are not retained unless you specify a retain statement or if you use a temporary array.  Since we can only see a fragment of your DATA step, it is not obvious how to answer your questions.

Ask a Question
Discussion stats
  • 5 replies
  • 408 views
  • 3 likes
  • 2 in conversation