Data Step-Creating a new Variable

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Data Step-Creating a new Variable

I have a data set  as

DATA Have;

input Name $  Age Height @@;

Datalines;

Adam 7 37

Adam 8 43

Jack 26 150

Jack 27 151

Jack 28 163

Jack 29 163

Joe 12 16

Joe 13  17

Joe 14 17

;

Now I want to create a new variable that serves as a counter for each Name. My final data should have the following form

I made a few silly attempts, non of them seem to work!

Thanks for your attention.


Accepted Solutions
Solution
‎03-27-2013 11:42 AM
Respected Advisor
Posts: 4,919

Re: Data Step-Creating a new Variable

Assuming Have is sorted by Name :

data want;

set have; by name;

if first.name then newVar = 0;

newVar + 1;

run;

PG

PG

View solution in original post


All Replies
Solution
‎03-27-2013 11:42 AM
Respected Advisor
Posts: 4,919

Re: Data Step-Creating a new Variable

Assuming Have is sorted by Name :

data want;

set have; by name;

if first.name then newVar = 0;

newVar + 1;

run;

PG

PG
Respected Advisor
Posts: 3,156

Re: Data Step-Creating a new Variable

The more, the merrier (I hope):

data want;

set have; by name;

newVar + 1+(-first.name*newvar);

run;

Haikuo

Regular Contributor
Posts: 151

Re: Data Step-Creating a new Variable

data want;

set have;

by name;

if first.name then count=0;

count+1;

run;

This assumes that your data is sorted (and that the names are unique!)

Contributor
Posts: 66

Re: Data Step-Creating a new Variable

Thanks for the help!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 224 views
  • 3 likes
  • 4 in conversation