DATA Step, Macro, Functions and more

combined counter for 2 variables

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

combined counter for 2 variables

Hi,

 

The outcome of my query should be the following :

 

genderscoreoutcome
1481
1481
1452
1563
2501
2501
2412
2513

 

This is my script :

 

data students;
  input gender score ;
  cards;
  1 48
  1 48
  1 45
  1 56
  2 50
  2 50
  2 41
  2 51
  ;
run;

 

I've tried this :

 

proc sort data = students;
  by gender score;
run;
data students2;
  set students;
  by gender score;
  if (first.gender and first.score) then outcome + 1;
run;

 

But this is not the solution.

 

Can anybody help out ?

 

Thanks

B


Accepted Solutions
Solution
‎01-23-2018 04:49 PM
PROC Star
Posts: 2,346

Re: combined counter for 2 variables

Posted in reply to Billybob73

Liek this?


data students2;
  set students;
  by gender score;
  if first.gender then outcome = 0;
  if first.score  then outcome + 1;
run;
gender score outcome
1 45 1
1 48 2
1 48 2
1 56 3
2 41 1
2 50 2
2 50 2
2 51 3

View solution in original post


All Replies
Solution
‎01-23-2018 04:49 PM
PROC Star
Posts: 2,346

Re: combined counter for 2 variables

Posted in reply to Billybob73

Liek this?


data students2;
  set students;
  by gender score;
  if first.gender then outcome = 0;
  if first.score  then outcome + 1;
run;
gender score outcome
1 45 1
1 48 2
1 48 2
1 56 3
2 41 1
2 50 2
2 50 2
2 51 3
Contributor
Posts: 55

Re: combined counter for 2 variables

Thanks very much Chris !

Super User
Posts: 23,700

Re: combined counter for 2 variables

Posted in reply to Billybob73

Store your FIRST variables to help build your conditions.

I've included the correct answer, but commented it out. 

 

data students;
  input gender score ;
  cards;
  1 48
  1 48
  1 45
  1 56
  2 50
  2 50
  2 41
  2 51
  ;
run;
 
I've tried this :
 
proc sort data = students;
  by gender score;
run;
data students2;
  set students;
  by gender score;
  first_gender = first.gender;
first_score = first.score;

/*
if first.gender then outcome=1;
else if first.score then outcome+1;
*/
run;
 

Billybob73 wrote:

Hi,

 

The outcome of my query should be the following :

 

gender score outcome
1 48 1
1 48 1
1 45 2
1 56 3
2 50 1
2 50 1
2 41 2
2 51 3

 

This is my script :

 

data students;
  input gender score ;
  cards;
  1 48
  1 48
  1 45
  1 56
  2 50
  2 50
  2 41
  2 51
  ;
run;

 

I've tried this :

 

proc sort data = students;
  by gender score;
run;
data students2;
  set students;
  by gender score;
  if (first.gender and first.score) then outcome + 1;
run;

 

But this is not the solution.

 

Can anybody help out ?

 

Thanks

B


 

Contributor
Posts: 55

Re: combined counter for 2 variables

Thanks Reeza!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 139 views
  • 0 likes
  • 3 in conversation