DATA Step, Macro, Functions and more

lead

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

lead

Hello,

 

I want to populate var2 with subsequent record value if the var1 is missing.

 

data test;
input var1 $ ;
cards;
.
abc
;
run;

data want;
  set test;
  var2=substr(var1,1,1);
run;

Desired output:

var1         var2

.                a

abc           a


Accepted Solutions
Solution
‎07-04-2017 09:33 AM
Super User
Posts: 6,926

Re: lead

Merge the dataset with itself, but start one observation later:

data want;
merge
  test
  test (firstobs=2 rename=(var1=var1x))
;
if var1 = ' '
then var2 = substr(var1x,1,1);
else var2 = substr(var1,1,1);
drop var1x;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-04-2017 09:33 AM
Super User
Posts: 6,926

Re: lead

Merge the dataset with itself, but start one observation later:

data want;
merge
  test
  test (firstobs=2 rename=(var1=var1x))
;
if var1 = ' '
then var2 = substr(var1x,1,1);
else var2 = substr(var1,1,1);
drop var1x;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 1 reply
  • 74 views
  • 1 like
  • 2 in conversation