DATA Step, Macro, Functions and more

Use last. first. with an array

Reply
New User
Posts: 1

Use last. first. with an array

 

I would like to use first. and last. with an array statement. It should work like this: 



data have; informat date mmddyy8.; format date date9.; input dose id $ supply date ; datalines; 5 1234 30 01012015 10 1234 30 02012015 10 1234 30 03012015 5 1234 30 04012015 2 1234 30 05012015 5 4321 30 07012016 2 9876 30 05012016 2 9876 30 06012016 10 9876 30 07012016 ; run; proc sort data=have; by id date; run; data want; set have; by id dose notsorted; retain n_days; array my_array[*] dose id;
do i=1 to dim(my_array); if first.myarray(i)then n_days=0; end; n_days+supply; if last.dose then output; run;

Since the real array contains more than 200 variables it is not possible to do this manually.

 

Super User
Posts: 6,785

Re: Use last. first. with an array

Well, two things to consider.

 

First, this problem has nothing to do with arrays.  Zero. 

 

Second, this problem requires understanding what your BY variables look like.  You could just as easily code:

 

if first.dose then n_days=0;

 

That would set n_days properly.  Whenever you hit the first observation for an ID, first.dose is true.

Valued Guide
Posts: 597

Re: Use last. first. with an array

Hi,

 

I don't understand why you want to use First. on all the 200 columns you have. 

 

Are you trying to calculate the variable values by dose,Id like:

Days1+Supply1;

Days2+Supply2;

.

.

.

.

.

.So on until all 200 variables 

Thanks,
Suryakiran
Super User
Super User
Posts: 8,117

Re: Use last. first. with an array

Your current example only has one variable, DOSE.  Can you post an example with an array of two or more variables?  

What is you want to do with this array?

Are you trying to sum each column (variable) in the array?

If so then you will need to a matching array of result variables rather than just the single N_DAYS variable.

Esteemed Advisor
Posts: 5,535

Re: Use last. first. with an array

[ Edited ]

What you are trying to do is not obvious from your code. But I think you should revise the workings of FIRST, LAST and BY variables.

 

Note that with

 

BY Var1 Var2 Var3;

 

first.Var3 is always 1 if either first.Var1 or first.Var2 is 1. So it might be enough for your purpose to monitor first.Var3.

PG
Valued Guide
Posts: 597

Re: Use last. first. with an array

Are you trying to do this type of calculation?

 

data have;
 input dose  id $ supply supply2 supply3 ;
 datalines;
5 1234 30 40 50
10 1234 30 40 50
10 1234 30 40 50
5 1234 30 40 50
2 1234 30 40 50
5 4321 30 40 50
2 9876 30 40 50
2 9876 30 40 50
10 9876 30 40 50
 ;
run;

proc sort data=have;
  by id dose;
run;

data want(DROP=Supply Supply2 Supply3);
set have;
by id dose;
IF first.id and first.dose then Do;
	Supply_1=supply;
	Supply_2=supply2;
	supply_3=supply3;
	end;
Else do;
Supply_1+Supply;
Supply_2+Supply2;
Supply_3+Supply3;
end;
if last.dose then output;
run;
Thanks,
Suryakiran
Ask a Question
Discussion stats
  • 5 replies
  • 180 views
  • 0 likes
  • 5 in conversation