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-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!

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
  • 1535 views
  • 1 like
  • 4 in conversation