BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ihsan-Mahdi
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Tom
Super User Tom
Super User

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;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 728 views
  • 3 likes
  • 3 in conversation