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

Hi!

 

I'm working with data like the first table below where I have multiple records per id, some are positive some are negative (1 and 0, respectively). I also know whether there was a previous record, where the first instance is not 1, but each row for that id number afterward is. I want to get a count variable like the second table which cumulatively counts each record per id number. 

I've tried some sql coding and a data step using the retain statement but can't get it quite right. Please help!

 

erin3_0-1627481411138.png

 

erin3_1-1627481432622.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

data want;
    set have;
    by id;
    if first.id then count_record=0;
    count_record+record;
run;
    

In the future, from now on (that's 100% of the time from now on) you need to post your data not as a screen capture, but as a SAS data step, by following these instructions and then people can provide you with TESTED code.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

data want;
    set have;
    by id;
    if first.id then count_record=0;
    count_record+record;
run;
    

In the future, from now on (that's 100% of the time from now on) you need to post your data not as a screen capture, but as a SAS data step, by following these instructions and then people can provide you with TESTED code.

--
Paige Miller
erin3
Calcite | Level 5

Perfect! Thank you. I'll ask questions per those instructions 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
  • 2 replies
  • 968 views
  • 1 like
  • 2 in conversation