The code works on the strict assumption that date/price variables are paired on their sequence numbers.
Note that because of the use of date: and price:, the order of the variables is important for a correct pairing; see this short illustration of the use of variable lists in ARRAY statements:
data have;
input a1 a2 a3 b1 b3 b2;
datalines;
1 2 3 4 5 6
;
data test;
set have;
array for_a {*} a:;
array for_b_1 {*} b:; /* ordered as positioned in the dataset */
array for_b_2 {*} b1-b3; /* ordered by name */
array for_b_3 {*} b1--b3; /* ordered by position, missing one item */
dim3 = dim(for_b_3);
put dim3;
do i = 1 to 3;
put i=;
name_a = vname(for_a{i});
put name_a=;
name_b_1 = vname(for_b_1{i});
put name_b_1=;
name_b_2 = vname(for_b_2{i});
put name_b_2=;
if i le 2
then do;
name_b_3 = vname(for_b_3{i});
put name_b_3=;
end;
end;
run;
The only method that forces a correct order by variable name, regardless of physical position in the dataset, is the x1-xX notation.
The fact that you wanted a total sum line for every PNR let me assume you need a report. Inserted summary observations in datasets usually makes those datasets unusable for further processing.
But you can easily summarize from the want dataset:
proc summary data=want;
by pnr notsorted;
var total_cost;
output
out=sum (drop=_type_ rename=(_freq_=num_years))
sum()=
;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: