DATA Step, Macro, Functions and more

Grouping?

Accepted Solution Solved
Reply
Super Contributor
Posts: 390
Accepted Solution

Grouping?

Hello:

I would like to group with years.   Please suggest how.

 

 

data test; 
      infile datalines dlm='|'; 
  input ID  Year State : $10.; 
datalines;                  
300012556 | 1998 |  GA |   
300012956 | 1985 |  IA |   
300012586 | 1963 |  CA |  
300052586 | 1997 |  TX | 
300052576 | 1999 |  OH |   
300052577 | 1999 |  TX |   
300052578 | 1999 |  NY |  
300012578 | 1998 |  NV |  
300012878 | 1999 |  FL | 
300022578 | 1985 |  PA |   
; 

Proc sort; by year; run;

 

 

 

 

My result would like to be:

Year       ID                       State

1963     300012586          CA

1985     300012956          IA

             300022578          PA

1997     300052586          TX

1998     300012556          GA

             300012578          NV

1999     300052576          OH

             300052577          TX

             300052578          NY

             300012878          FL

 


Accepted Solutions
Solution
‎03-28-2018 10:31 AM
Super User
Posts: 13,023

Re: Grouping?

It looks like you want a REPORT not a dataset.

 

proc report data=test nowd;
   column year id state;
   define year /group;
   define id/display;
   define state/display;
run;

If you do want a data set it really makes much more sense to keep the year on each record, such as building reports like this.

 

View solution in original post


All Replies
Solution
‎03-28-2018 10:31 AM
Super User
Posts: 13,023

Re: Grouping?

It looks like you want a REPORT not a dataset.

 

proc report data=test nowd;
   column year id state;
   define year /group;
   define id/display;
   define state/display;
run;

If you do want a data set it really makes much more sense to keep the year on each record, such as building reports like this.

 

Trusted Advisor
Posts: 1,284

Re: Grouping?

You've sorted the data by year  (although you may want to sort "by year state").  And now you want to report it with each year posted only for its first instance.

 

Look at PROC PRINT,  with the BY statement, and the ID statement.  Show us what your found that works, and mark your own successful answer as the solution.

Super Contributor
Posts: 390

Re: Grouping?

Thanks for all of your great suggestion.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 73 views
  • 1 like
  • 3 in conversation