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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.