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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1622 views
  • 0 likes
  • 2 in conversation