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

Hi all!

I have a counting visit question 🙂  For ONE subject, if I had visits A1 A3 D1 LD  and again A1 A2 A3 D1  (all on different dates).  Whenever encountering 'A1', i want to increase the count variable.

So the sorted visits look like 

A1 A3 D1 LD  A1 A2 A3 D1

and the counting sequence I want is 1 1 1 1 2 2 2 2

 

Here is my failed attempt:

data endptx;
set endpt00;
by usubjid vst_num vsdat;
count=0;
if vst_num='A1' then count+1;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

I suspect that you want to reset the count to 0 when each USUBJID is encountered.

 

data endptx;
set endpt00;
by usubjid vst_num vsdat;
if first.usubjid then count=0;
if vst_num='A1' then count+1;

run;

If that does not provide what you need then you will need to provide some example data for input and the expected output for the given example. Best is to provide the example data in the form of data step code.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

View solution in original post

2 REPLIES 2
ballardw
Super User

I suspect that you want to reset the count to 0 when each USUBJID is encountered.

 

data endptx;
set endpt00;
by usubjid vst_num vsdat;
if first.usubjid then count=0;
if vst_num='A1' then count+1;

run;

If that does not provide what you need then you will need to provide some example data for input and the expected output for the given example. Best is to provide the example data in the form of data step code.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

Patrick
Opal | Level 21

What about:

data endptx;
  set endpt00;
  by usubjid vst_num vsdat;
  if first.usubjid then count=0;
  if vst_num='A1' then count+1;
run;

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