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

Hi Everyone,

 

I have a set of data in long form that contains missing data. I'd like to collapse by the ID and have 1 row per ID.

 

 

Here is what I have:

 

IDInitialCountFinalCount
10.
1..
1..
1..
1.0
23.
2..
2..
2..
2.0
30.
3..
3..
3.7

 

Here is what I want:

 

IDInitialCountFinalCount
100
230
307

 

Any help would be greatly appreciated! Thanks in advance! 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The easiest program might be:

 

proc summary data=have nway;

  by id;

  var InitialCount FinalCount;

  output out=want (drop=_type_ _freq_) max=;

run;

 

You can use a more complex program that doesn't require you to specify the variable names:

 

data want;

update have (obs=0) have;

by id;

run;

 

For now, I suspect that learning more about PROC SUMMARY would be useful.

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Can you be sure that your data always has only 1 nonmissing obervation for InitialCount and 1 nonmissing obervation for FinalCount for each ID?

hwangnyc
Quartz | Level 8
Hi Draycut, Yes, that is how I setup the two variables.
PeterClemmensen
Tourmaline | Level 20

ok. There u go 🙂

 

data have;
input ID	InitialCount FinalCount;
datalines;
1	0	.
1	.	.
1	.	.
1	.	.
1	.	0
2	3	.
2	.	.
2	.	.
2	.	.
2	.	0
3	0	.
3	.	.
3	.	.
3	.	7
;

proc sort data = have;
   by ID;
run;

data help;
   set have;
   by ID;
   if first.ID or last.ID then output;
run;

data want(drop = lag);
   set help;
   by ID;

   lag = lag(InitialCount);

   if last.ID then do;
      InitialCount = lag;
      output;
   end;
run;
Astounding
PROC Star

The easiest program might be:

 

proc summary data=have nway;

  by id;

  var InitialCount FinalCount;

  output out=want (drop=_type_ _freq_) max=;

run;

 

You can use a more complex program that doesn't require you to specify the variable names:

 

data want;

update have (obs=0) have;

by id;

run;

 

For now, I suspect that learning more about PROC SUMMARY would be useful.

PeterClemmensen
Tourmaline | Level 20

Cool solutions @Astounding 🙂

ballardw
Super User

proc summary data=have nway;

   class id;

   var initialcount finalcount;

   output out=want (drop= _:) max= ;

run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2556 views
  • 1 like
  • 4 in conversation