BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mingjueca
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

8 REPLIES 8
mingjueca
Calcite | Level 5
win count. Win count is exact same as counter.
Kurt_Bremser
Super User

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.

mingjueca
Calcite | Level 5
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.
Kurt_Bremser
Super User

@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.

mingjueca
Calcite | Level 5
You are right, I switched sequence of statements. thanks a lot
Reeza
Super User

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;

 


 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 8 replies
  • 1666 views
  • 0 likes
  • 4 in conversation