DATA Step, Macro, Functions and more

Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

[ Edited ]

Hello,

I am new to the SAS community and am getting started with learning.

I have a set of great books and especially like The Little SAS Book and its companion, 'Exercises and Projects for The Little SAS Book'

 

I am looking for solution for Chapter 3 Programming exercise 33 

             f : Create another cumulative counter variable that tracks the number of wins for each team.

 

I already used retain statement but it didn't work. 

 

data sol33;
infile 'G:\Exercise\RoseBowl.dat' ;
input game_date mmddyy10. team $22. @38 score 2. @41 lost_team $22. lost_score ;
diff=score-lost_score;
game_day=weekday(game_date);
counter+1;
retain pre_team;
output;
pre_team=team;
if pre_team =team then win_count+1;
run;

 


Accepted Solutions
Solution
‎12-17-2017 06:11 PM
Super User
Posts: 23,773

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to mingjueca

Personally, I prefer to separate my input and analysis/data manipulation steps. 

 

Breaking it down into the steps you need:

 

1. Import data and make sure it's properly read in. 

2. Make sure the data is sorted by team and date so that you can do analysis by each team

3. Use FIRST & LAST to identify first and last of each group

4. At first of each group, reset your counters

5. Increment the counter for each win

 

This is a slightly more difficult problem than the one here:

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

If you still can't get it, post back your next attempt and we can help with showing where it needs modification.


mingjueca wrote:

Hello,

I am new to the SAS community and am getting started with learning.

I have a set of great books and especially like The Little SAS Book and its companion, 'Exercises and Projects for The Little SAS Book'

 

I am looking for solution for Chapter 3 Programming exercise 33 

             f : Create another cumulative counter variable that tracks the number of wins for each team.

 

I already used retain statement but it didn't work. 

 

data sol33;
infile 'G:\Exercise\RoseBowl.dat' ;
input game_date mmddyy10. team $22. @38 score 2. @41 lost_team $22. lost_score ;
diff=score-lost_score;
game_day=weekday(game_date);
counter+1;
retain pre_team;
output;
pre_team=team;
if pre_team =team then win_count+1;
run;

 


 

View solution in original post


All Replies
PROC Star
Posts: 1,283

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to mingjueca

What part of the result is not as you expected?

Occasional Contributor
Posts: 5

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

win count. Win count is exact same as counter.
Super User
Posts: 10,280

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to mingjueca

Look closely at this part of your code:

pre_team=team;
if pre_team =team then win_count+1;

The condition can only be true. You need to work with the sequence of those statements, and where to put the output statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to KurtBremser
You are right, the condition all is true. but I just don't know how to fix this. If it is possible, could you give me the details. thanks in advance.
Super User
Posts: 10,280

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to mingjueca

mingjueca wrote:
You are right, the condition all is true. but I just don't know how to fix this. If it is possible, could you give me the details. thanks in advance.

As I already said, change the sequence of statements. Imagine yourself as the SAS system and "run" your code. If necessary, write the values of the variables down for every step of the datastep code.

Or insert put _all_ statement to display the values.

 

This is not a really difficult problem, you can solve it. Believe in yourself.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to KurtBremser
You are right, I switched sequence of statements. thanks a lot
Solution
‎12-17-2017 06:11 PM
Super User
Posts: 23,773

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

Posted in reply to mingjueca

Personally, I prefer to separate my input and analysis/data manipulation steps. 

 

Breaking it down into the steps you need:

 

1. Import data and make sure it's properly read in. 

2. Make sure the data is sorted by team and date so that you can do analysis by each team

3. Use FIRST & LAST to identify first and last of each group

4. At first of each group, reset your counters

5. Increment the counter for each win

 

This is a slightly more difficult problem than the one here:

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

If you still can't get it, post back your next attempt and we can help with showing where it needs modification.


mingjueca wrote:

Hello,

I am new to the SAS community and am getting started with learning.

I have a set of great books and especially like The Little SAS Book and its companion, 'Exercises and Projects for The Little SAS Book'

 

I am looking for solution for Chapter 3 Programming exercise 33 

             f : Create another cumulative counter variable that tracks the number of wins for each team.

 

I already used retain statement but it didn't work. 

 

data sol33;
infile 'G:\Exercise\RoseBowl.dat' ;
input game_date mmddyy10. team $22. @38 score 2. @41 lost_team $22. lost_score ;
diff=score-lost_score;
game_day=weekday(game_date);
counter+1;
retain pre_team;
output;
pre_team=team;
if pre_team =team then win_count+1;
run;

 


 

Occasional Contributor
Posts: 5

Re: Need Solutions for Chapter 3 Question 33 in 'Exercises and Projects for The Little SAS Book'

thanks a lot
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 236 views
  • 0 likes
  • 4 in conversation