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
PROC Star

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.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 7 replies
  • 1104 views
  • 2 likes
  • 5 in conversation