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

Hi,

 

I have a dataset looks like below:

 

ID   Visit    CurrentTotal    

1      0             0                 

1      0             1                  

1      1             0                 

1      0             3                 

1      1             2                 

1      1             1                

1      1             0                

 

For the same ID,  there are three parts of data - the observations in black, red and blue. For each part, I need to count the total number of visits. Right now, the CurrentTotal variable is displayed in the dataset, which is not correct for the counts of each part (black, red and blue). What I would like to ask is that "How to add a variable "TargetTotal" as shown below to have the correct number of visit count for each part? How can the SAS codes be able to rank three orders within the same ID?" The order should start with 0, and end till the next 0, so there are three parts (the black, red and blue t) for the same ID.

 

ID   Visit    CurrentTotal    TargetTotal

1      0             0                 0

1      0             1                 0 

1      1             0                 1

1      0             3                 0

1      1             2                 1

1      1             1                 2

1      1             0                 3

 

 Thank you very much. Looking forward to the solutions. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Assuming each part of your data starts at Visit=0 :

 

data have;
input ID   Visit;
datalines;
1      0   
1      0   
1      1   
1      0   
1      1   
1      1   
1      1   
;

data want;
do until(last.id);
    set have; by id;
    if visit = 0 then rank = 0;
    else rank = rank + 1;
    output;
    end;
run;

proc print noobs; run;
PG

View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

Assuming each part of your data starts at Visit=0 :

 

data have;
input ID   Visit;
datalines;
1      0   
1      0   
1      1   
1      0   
1      1   
1      1   
1      1   
;

data want;
do until(last.id);
    set have; by id;
    if visit = 0 then rank = 0;
    else rank = rank + 1;
    output;
    end;
run;

proc print noobs; run;
PG
Astounding
Opal | Level 21

The simplest version would rely on VISIT only, to create TargetTotal:

 

data want;

set have;

if visit=0 then TargetTotal=0;

else TargetTotal + 1;

run;

 

Note that you don't have to worry about ID, when the first observation for each ID has VISIT=0.  The program checks for VISIT=0, which automatically includes the first observation for each ID.

cpulala
Obsidian | Level 7
Thanks! It works. But when the dataset has multiple IDs, a 'by ID;' statement need to be used.
Ksharp
Super User

data have;
input ID   Visit;
datalines;
1      0   
1      0   
1      1   
1      0   
1      1   
1      1   
1      1   
;
data temp;
 set have;
 by id;
 if first.id or visit=0 then group+1;
run;
data want;
 set temp;
 by group;
 if first.group then total=-1;
 total+1;
run;


cpulala
Obsidian | Level 7

Thanks! All responses work. It is hard to decide which one is the best solution; I choose the answer with the earliest response. 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 944 views
  • 2 likes
  • 5 in conversation