DATA Step, Macro, Functions and more

how to display only first instance of repeating data

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

how to display only first instance of repeating data

If I pull data that looks like this:

ID         Name         Gender          Date

1           Dan              M                1/1/2012

1           Dan              M               12/23/2011

1           Dan              M               10/15/2011

1           Dan              M                9/17/2011

2           Carol            F                 8/12/2011

2           Carol            F                 7/15/2011

is there a way to make it display like this:

ID         Name          Gender          Date

1           Dan            M                 1/1/2012

1                                                12/23/2011

1                                                10/15/2011

1                                                 9/17/2011

2          Carol           F                  8/12/2011

2                                                7/15/2011

I want to not display most repeating data except for the ID so if the data gets sorted a different way they can always get back to the original. I think I read about a way to do this but now I can't find where I read it.

Thanks     


Accepted Solutions
Solution
‎03-15-2012 10:54 AM
Respected Advisor
Posts: 3,124

how to display only first instance of repeating data

Try this:

data have;

input (id Name Gender Date) (:$10.);

cards;

1           Dan              M                1/1/2012

1           Dan              M               12/23/2011

1           Dan              M               10/15/2011

1           Dan              M                9/17/2011

2           Carol            F                 8/12/2011

2           Carol            F                 7/15/2011

;

data want;

set have;

by id;

name=ifc(first.id,name,' ');

gender=ifc(first.id,gender,' ');

run;

Regards,

Haikuo

View solution in original post


All Replies
Solution
‎03-15-2012 10:54 AM
Respected Advisor
Posts: 3,124

how to display only first instance of repeating data

Try this:

data have;

input (id Name Gender Date) (:$10.);

cards;

1           Dan              M                1/1/2012

1           Dan              M               12/23/2011

1           Dan              M               10/15/2011

1           Dan              M                9/17/2011

2           Carol            F                 8/12/2011

2           Carol            F                 7/15/2011

;

data want;

set have;

by id;

name=ifc(first.id,name,' ');

gender=ifc(first.id,gender,' ');

run;

Regards,

Haikuo

Contributor
Posts: 37

Re: how to display only first instance of repeating data

That works. Thank you Haikuo.

Super User
Posts: 5,082

how to display only first instance of repeating data

Take a look at the ID statement within PROC PRINT.

Also consider whether you want the variable named ID to repeat or to print just once.

Good luck.

SAS Super FREQ
Posts: 8,743

Re: how to display only first instance of repeating data

Hi:

   The posted solution will create a DATASET with the desired change. However, if all you want is a REPORT -- such as an HTML, RTF, PDF, etc, report, then PROC REPORT will do what you want without making an extra copy of the data.

cynthia

data fakedata;

infile datalines;

input idvar  Name $ Gender $ Date : mmddyy10.;

return;

datalines;

1    Dan     M    9/17/2011

1    Dan     M    5/14/2010

1    Dan     M     1/1/2012

1    Dan     M   12/23/2011

1    Dan     M   10/15/2011

2    Carol   F    8/12/2011

2    Carol   F    7/15/2011

3    Fred    M    5/15/2011

3    Fred    M   11/15/2011

3    Fred    M   12/28/2011

;

run;

   

proc  sort data=fakedata;

  by idvar name gender descending date ;

run;

 

ods html file='c:\temp\report.html';

  

proc report data=fakedata nowd;

  column idvar name gender date;

  define idvar / display 'ID';

  define Name / order 'Name' order=data;

  define Gender / order 'Gender' order=data;

  define date / order order=data f=mmddyy10.;

run;

 

ods html close;


proc_report_order_usage.jpg
Contributor
Posts: 37

Re: how to display only first instance of repeating data

It is a dataset that I'm interested in but I'll keep this for future reference. Thank you.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 181 views
  • 3 likes
  • 4 in conversation