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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

3 REPLIES 3
Reeza
Super User

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

Astounding
PROC Star

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;

Ksharp
Super User

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;

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
  • 3 replies
  • 1034 views
  • 1 like
  • 4 in conversation