BookmarkSubscribeRSS Feed
gklsc
Calcite | Level 5

 

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.

 

5 REPLIES 5
Astounding
PROC Star

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.

SuryaKiran
Meteorite | Level 14

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
Tom
Super User Tom
Super User

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.

PGStats
Opal | Level 21

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
SuryaKiran
Meteorite | Level 14

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 2267 views
  • 0 likes
  • 5 in conversation