BookmarkSubscribeRSS Feed
evat
Calcite | Level 5

Hi! I'm a long-time stata user and new to sas. I'm trying to figure out how to create a new variable based on the sum of the values of another variable.

 

In stata, I can do this using the "egen" command. For example,

 

gen CSI1strong = 1 if csi1>5 

egen csi1strong=sum(CSI1strong), by(survey) 

 

The first line of code creates a new variable, CSI1strong, that is equal to 1 if csi1>5

The second line of code creates a new variable, csi1strong, that is equal to the sum of CSI1strong for all rows of data, by survey. 

 

 

How can I reproduce this in SAS?

 

Thank you in advance for your help!

Eva

1 REPLY 1
ballardw
Super User

@evat wrote:

Hi! I'm a long-time stata user and new to sas. I'm trying to figure out how to create a new variable based on the sum of the values of another variable.

 

In stata, I can do this using the "egen" command. For example,

 

gen CSI1strong = 1 if csi1>5 

egen csi1strong=sum(CSI1strong), by(survey) 

 

The first line of code creates a new variable, CSI1strong, that is equal to 1 if csi1>5

The second line of code creates a new variable, csi1strong, that is equal to the sum of CSI1strong for all rows of data, by survey. 

 

 

How can I reproduce this in SAS?

 

Thank you in advance for your help!

Eva


It would help SAS users more to provide an example of the starting data, the desired result and a description of what goes on if the two examples aren't very straight forward.

SUMing a variable in groups can be done in Proc Means or summary with the groups defined by CLASS variable(s), or in Proc Sql with a Group by.

Then combine the results, how will depend on the data sets.

I don't speak STATA, so that syntax means nothing.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 1656 views
  • 0 likes
  • 2 in conversation