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

I'm trying to create a new column that counts the number of practice sessions between each optional practice session, but I want it to reset the counts between each two optional sessions and not aggregate. How can I do this? I know how to count it by aggregating the observations, but not how to reset them between each optional session.

data have;
  input ID $Practice;
 datalines;
1 Mandatory
2 Mandatory
3 Optional
4 Optional
5 Mandatory
6 Mandatory
7 Mandatory
8 Mandatory
9 Optional
10 Mandatory
11 Mandatory
12 Optional

I basically want a dataset that looks like this

 

ID Practice   Count 

1 Mandatory    1 
2 Mandatory    2
3 Optional        2
4 Optional       0
5 Mandatory    1
6 Mandatory    2
7 Mandatory    3  
8 Mandatory    4
9 Optional       4
10 Mandatory  1
11 Mandatory   2
12 Optional      2

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You want to:

  1. Prior to checking the current practice, reset count to 0 if the preceding practice is optional.
  2. Increment count by 1 if the current practice is not optional.

 

 

data want;
  set have; 
  if lag(practice)='Optional' then count=0;
  count+(practice^='Optional');
run;

 

v

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

You want to:

  1. Prior to checking the current practice, reset count to 0 if the preceding practice is optional.
  2. Increment count by 1 if the current practice is not optional.

 

 

data want;
  set have; 
  if lag(practice)='Optional' then count=0;
  count+(practice^='Optional');
run;

 

v

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nharuka
Obsidian | Level 7

Hi V,

 

Your code didn't work. When I tried to use it, it gave me "." for most of the mandatory cells and then "0" for the mandatory rows that comes right after the optional rows instead. I checked my log as well and there were no error messages so I'm not sure what went wrong.

nharuka
Obsidian | Level 7

Please disregard this, it worked.

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
  • 3 replies
  • 666 views
  • 0 likes
  • 2 in conversation