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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 448 views
  • 0 likes
  • 3 in conversation