Hello,
I have a data set that looks something like this:
DATA example;
INPUT id ais;
DATALINES;
1 3
1 4
2 5
2 7
2 3
3 2
3 4
3 1
;
run;
I would like to square each "ais" value, then add the squares by each "id" value creating a new variable called "iss".
so for "id" (1) the iss should equal 9+16=25.
I tried different codes but none worked. Help please!
Assuming you want to output 3 observations, one per BY group, then something like this should do the job.
data want;
do until(last.id);
set example;
by id;
iss = sum(iss,ais**2);
end;
keep id iss;
run;
This is a job for PROC SUMMARY. No need to write your own DATA step code with a mathematical formula.
proc summary data=example nway;
class id;
var ais;
output out=want uss=sum_of_squares;
run;
Assuming you want to output 3 observations, one per BY group, then something like this should do the job.
data want;
do until(last.id);
set example;
by id;
iss = sum(iss,ais**2);
end;
keep id iss;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.