SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Keegan
Obsidian | Level 7

I have a data set that I'm trying to add a count variable for. Here is s my dataset:

key date type
1234 1/28/2022 A
1234 2/15/2022 A
1234 2/28/2022 A
1234 3/6/2022 B
1234 4/1/2022 B
1234 4/15/2022 C
1234 5/1/2022 C
1133 12/21/2021 A
1133 1/20/2022 A
1133 3/18/2022 B
1133 4/4/2022 C
1133 5/3/2022 C
3434 2/16/2022 A
3434 3/10/2022 A
3434 4/3/2022

C

 

I've tried to use the following code:

data add_count;
	set test_data;
	count +1;
	by type;

if first.type then count=1;
run;

However, this does not work. I get the following error:

ERROR: BY variables are not properly sorted on data set WORK.test_data.

 

I need to count the type, for each key in the order they are sorted by date. If I add in the "date" to the by statement in the code above, then my count is just 1 for every observation, since the dates are different.

How can I get this output:

key date type count
1234 1/28/2022 A 1
1234 2/15/2022 A 2
1234 2/28/2022 A 3
1234 3/6/2022 B 1
1234 4/1/2022 B 2
1234 4/15/2022 C 1
1234 5/1/2022 C 2
1133 12/21/2021 A 1
1133 1/20/2022 A 2
1133 3/18/2022 B 1
1133 4/4/2022 C 1
1133 5/3/2022 C 2
3434 2/16/2022 A 1
3434 3/10/2022 A 2
3434 4/3/2022 C 1

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You need to tell the data step that the you want the BY statement to work even though the data is NOT sorted by those variables.

Use the NOTSORTED keyword.

data add_count;
  set test_data;
  by type notsorted;
  count +1;
  if first.type then count=1;
run;

 

If you want the numbering to reset also when KEY changes that add that variable to the BY statement also.

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Try this:

 

	by key notsorted type;

 

This assumes that all values of KEY are consecutive, and that all values of TYPE are consecutive.

--
Paige Miller
Tom
Super User Tom
Super User

You need to tell the data step that the you want the BY statement to work even though the data is NOT sorted by those variables.

Use the NOTSORTED keyword.

data add_count;
  set test_data;
  by type notsorted;
  count +1;
  if first.type then count=1;
run;

 

If you want the numbering to reset also when KEY changes that add that variable to the BY statement also.

 

Keegan
Obsidian | Level 7

Wow, that was so easy.. Thank you, that worked perfectly!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1337 views
  • 0 likes
  • 3 in conversation