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

Hello, 

I'm looking for a function that would return the first value in a row of variables and one that will return that last value in the row. For example, if I have a data set like this:

 

var1var2var3var4var5var6var7var8
21743569
461015232100
152264298120

 

I'd like to create two new variables (call them first and last) that have the first and last values in each row, so I'd end up with this (relevant values colored for clarity):

 

var1var2var3var4var5var6var7var8firstlast
2174356929
46101523210040
1522642981201520

 

Any help would be appreciated. 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input var1-var8;
datalines;
. 1 7 . 3 5 6 9
4 6 . 15 . 2 10 0
15 22 6 . 2 98 1 .
;

data want;
   set have;
   first = coalesce(of var1-var8);
   last = coalesce(of var8-var1);
run;

 Edited post from brute force to this 🙂

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20
data have;
input var1-var8;
datalines;
2 1 7 4 3 5 6 9
4 6 10 15 23 2 10 0
15 22 6 4 2 98 1 20
;

data want;
   set have;
   array a {*} var1-var8;
   first = a[lbound(a)];
   last  = a[hbound(a)];
run;

 

Result:

 

var1 ............... var8  first last 
2  1  7  4  3  5  6  9     2     9 
4  6  10 15 23 2  10 0     4     0 
15 22 6  4  2  98 1  20    15    20 

 

novinosrin
Tourmaline | Level 20
 
data have;
 input var1	var2	var3	var4	var5	var6	var7	var8;
 cards;
2	1	7	4	3	5	6	9
4	6	10	15	23	2	10	0
15	22	6	4	2	98	1	20
;

data want;
 set have;
 array v var1-var8;
 first=v(1);
 last=v(dim(v));
run;
mt88
Calcite | Level 5

Thank you, that worked. As a follow up, I'm wondering how to do something similar when there is missing data present. For example, starting with a similar data set, except I've added in some missings:

 

var1var2var3var4var5var6var7var8
.17.3569
46.15.2100
15226.2981.

 

I'd like to end up with a similar results, except where the missing values are not included (relevant values colored for clarity):

var1var2var3var4var5var6var7var8firstlast
.17.356919
46.15.210040
15226.2981.151

 

Thank you.

novinosrin
Tourmaline | Level 20

I am lazy , here is my lazy solution-

 


data have;
input var1	var2	var3	var4	var5	var6	var7	var8;
cards;
.	1	7	.	3	5	6	9
4	6	.	15	.	2	10	0
15	22	6	.	2	98	1	.
;


data want;
 set have;
 array v var1-var8;
 array vv var8-var1;
 first=coalesce(of v(*));
 last=coalesce(of vv(*));
run;

I was spoiled by @data_null__  teaching me the reverse technique 🙂

PeterClemmensen
Tourmaline | Level 20
data have;
input var1-var8;
datalines;
. 1 7 . 3 5 6 9
4 6 . 15 . 2 10 0
15 22 6 . 2 98 1 .
;

data want;
   set have;
   first = coalesce(of var1-var8);
   last = coalesce(of var8-var1);
run;

 Edited post from brute force to this 🙂

mt88
Calcite | Level 5

Thank you all of the help!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1878 views
  • 0 likes
  • 3 in conversation