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

This is sample of a dataset in the long format:

IDage
1131
10
10
222
276
30
348
348
33

 

We then ran the following to get the frequencies of every ID by age 

proc freq data=dsin;

table ID*age;

run;

And the output looks like:

 age     
ID03224876131
1200001
2001010
3110200

But now we also need to get the cumulative number of people who had an incident up until every age. The tricky part is that once a person has an incident and it is accounted for, if this person has incidents after that, it doesn't contribute to increasing the count anymore. Let's say for ID3 it presents an incident at age 0, 3 and 48. But this person can only contributes to the count at age 0. And also, if they have more than one incident at the same age, it counts as 1. The output should look like this:

Age Cumulative
02
32
223
483
763
1313

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

I am not sure, that is understand your request completely. The following steps seem to create what you expect. In future questions please post data in usable form.

 

data have;
   input ID age;
   datalines;
1 131
1 0
1 0
2 22
2 76
3 0
3 48
3 48
3 3
;

/* Get lowest age for each id */
proc sql;
   create table work.selection as
      select distinct Id, Age
         from work.have
         group by Id
         having Age = min(Age)
         order by Age
      ;
quit;

/* Get all ages */
proc sort data= work.have(keep= Age) out= work.ages nodupkey;
   by Age;
run;

/* Combine and count */
data want;
   merge work.ages work.selection(keep= Age in= inc);
   by Age;

   if inc then Cumulative + 1;

   if last.Age then output;
run;

View solution in original post

6 REPLIES 6
ballardw
Super User

I can guess that "had an incident" means every age recorded is an incident. What I can't decipher is what "up until every age" or " it is accounted for" means.

 

"Let's say for ID3 it presents an incident at age 0, 3 and 48. But this person can only contributes to the count at age 0." WHY? Exactly what in the data tells us to stop considering at 0?

ANKH1
Pyrite | Level 9
So at each age you sum the IDs that have values not equal to zero. Values 1,2,3 at each age means, that at that age an incident was reported. We want to know at each age how many IDs have accumulated first incidents. But if an ID reports incidents at more than one age, this ID will be accounted when their first incident is reported.
andreas_lds
Jade | Level 19

I am not sure, that is understand your request completely. The following steps seem to create what you expect. In future questions please post data in usable form.

 

data have;
   input ID age;
   datalines;
1 131
1 0
1 0
2 22
2 76
3 0
3 48
3 48
3 3
;

/* Get lowest age for each id */
proc sql;
   create table work.selection as
      select distinct Id, Age
         from work.have
         group by Id
         having Age = min(Age)
         order by Age
      ;
quit;

/* Get all ages */
proc sort data= work.have(keep= Age) out= work.ages nodupkey;
   by Age;
run;

/* Combine and count */
data want;
   merge work.ages work.selection(keep= Age in= inc);
   by Age;

   if inc then Cumulative + 1;

   if last.Age then output;
run;
ANKH1
Pyrite | Level 9

Thank you! I will post data in usable form in the future.

Ksharp
Super User
data have;
infile cards truncover expandtabs;
value=1;
input ID	age;
cards;
1	131
1	0
1	0
2	22
2	76
3	0
3	48
3	48
3	3
;
proc sort data=have out=temp nodupkey;
by age id;
run;
proc transpose data=temp out=temp2(drop=_name_) prefix=_;
by age;
id id;
var value;
run;
data temp2;
 set temp2;
 id=1;
run;
data temp3(drop=id);
 update temp2(obs=0) temp2;
 by id;
 output;
run;
data want;
 set temp3;
 Cumulative=sum(of _:);
run;
Tom
Super User Tom
Super User

So you seem to want to count the subjects at their first incident age.  So first reduce the data to just the minimum age per subject.

data have;
   input ID age;
datalines;
1 131
1 0
1 0
2 22
2 76
3 0
3 48
3 48
3 3
;

proc means data=have ;
  by id ;
  var age ;
  output out=cases(drop=_freq_ _type_ id) min=;
run;

proc sort data=cases ;
  by age;
run;

You seem to want to include all of the other age points in your report so get that from the original data also and then combine the two.


proc sort data=have(keep=age) out=age_points nodupkey;
  by age;
run;

data want;
  merge cases(in=incases) age_points;
  by age;
  cummulative + incases;
  if last.age;
run;

Result

Obs    age    cummulative

 1       0         2
 2       3         2
 3      22         3
 4      48         3
 5      76         3
 6     131         3

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
  • 6 replies
  • 621 views
  • 0 likes
  • 5 in conversation