Hello, I have a data set like:
A B
1 11
2 12
3 13
4 0
5 0
6 0
7 0
The goal is to have SAS search for all locations of b=0 and enter instead the lag of b, draging it throughout all "0"s, to get
1 11
2 12
3 13
4 13
5 13
6 13
7 13
I'm using the code
data test1;
set test;
x=lag(b);
if b=0 then b=x;
run;
Only b in line 4 gets to be 13, not the following rows, in them b stays 0.
Any help will be appriciated. This data set is an example, we do not know how many row there are with b>0 and how many with b=0, those rows can be at the end but also in other placed in the data set. Thank you!
It's simpler to create a new variable:
data want;
set have;
retain C;
if b ne 0 then c=b;
drop b;
rename c=b;
run;
C contains the proper values. So the DROP statement gets rid of the original variable B. And the RENAME statement changes the variable name from C (with proper values) to B (which is no longer being used).
It's simpler to create a new variable:
data want;
set have;
retain C;
if b ne 0 then c=b;
drop b;
rename c=b;
run;
C contains the proper values. So the DROP statement gets rid of the original variable B. And the RENAME statement changes the variable name from C (with proper values) to B (which is no longer being used).
works great, thank you!
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.
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.