DATA Step, Macro, Functions and more

Re: Selection Across Row

Accepted Solution Solved
Reply
Regular Contributor
Posts: 222
Accepted Solution

Re: Selection Across Row

Hi....I want to be able to select the last value across each row for each row. Is it possible to do the selection without having to transpose the dataset. Thanks.

 

Have:

Column1 Column2 Column3 Column4 Column5
201011 201112 201213    
201213 201314      
201516        
201112 201213 201314 201415 201516

 

Want:

Column
201213
201314
201516
201516

 


Accepted Solutions
Solution
‎09-01-2016 07:38 PM
Super User
Posts: 9,681

Re: Selection Across Row


data have;
infile cards expandtabs truncover;
input Column1	Column2	Column3	Column4	Column5;
cards;
201011	201112	201213	 	 
201213	201314	 	 	 
201516	 	 	 	 
201112	201213	201314	201415	201516
;
run;
data want;
 set have;
 want=coalesce(of Column5-Column1);
run;

View solution in original post


All Replies
Super User
Posts: 17,836

Re: Selection Across Row

What's the data type? If it's date or character AND as in example is increasing, use the MAX() function. 

Super User
Posts: 5,083

Re: Selection Across Row

You should be able to use arrays.  It's easier to read if you adjust the code for your variables being numeric or character (whichever they happen to be), but it can be done without necessarily knowing:

 

data want;

set have;

array cols {5} column1-column5;

do _n_=1 to 5;

   if not missing(cols{_n_}) then column = cols{_n_};

end;

run;

Solution
‎09-01-2016 07:38 PM
Super User
Posts: 9,681

Re: Selection Across Row


data have;
infile cards expandtabs truncover;
input Column1	Column2	Column3	Column4	Column5;
cards;
201011	201112	201213	 	 
201213	201314	 	 	 
201516	 	 	 	 
201112	201213	201314	201415	201516
;
run;
data want;
 set have;
 want=coalesce(of Column5-Column1);
run;

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 398 views
  • 1 like
  • 4 in conversation