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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 358 views
  • 1 like
  • 2 in conversation