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;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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 lock in 2025 pricing—just $495!

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
  • 879 views
  • 3 likes
  • 3 in conversation