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!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.