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

Hi all,

 

I am still quite new with SAS and I am currently trying to derive a new column in which each row is pretty much the increment that the other row increased by. So for instance, if I have data 'X', I am trying to derive column 'Y':

 

  X                    Y
100                100

350                250

760                410 

900                140

1200              300

2000              800

 

I am currently using this code:

 

data want;

set have;

 

Y=dif(X);

 

by /*insert personal sorted hierarchy*/ (first.ID,.,dif(X));

 

run;

 

Since I am trying to derive this column for different IDs, I order them to have the increments change with each new ID etc. This works well, however the problem I get is that the very first derived row is a blank for each new ID. However I would like to have the first row of Y to be the same as X shown above instead of a blank.

 

If anyone could please assist me, that would be great!

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
/*No ID var*/
data want;
set have;
y=ifn(_n_=1,x,dif(x));
run;
/*for ID var*/
data want;
set have;
by id;
y=ifn(first.id,x,dif(x));
run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
/*No ID var*/
data want;
set have;
y=ifn(_n_=1,x,dif(x));
run;
/*for ID var*/
data want;
set have;
by id;
y=ifn(first.id,x,dif(x));
run;
maguiremq
SAS Super FREQ

A less elegant way than above:

 

data have;
input x;
datalines;
100
350
760
900
1200
2000
;
run;

data want (drop=y rename=(y2=y));
set have;
y = x;
y2 = x-lag(x);
if y2 = . then y2 = y;
run;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1060 views
  • 0 likes
  • 3 in conversation