I have a dataset in which there a 3 columns.
A B C
1 . 1
1 . 2
1 . 3
1 0
2 . 1
2 . 2
2 0
3 . 1
3 . 2
3 0
So question is like this when B=0 then we have to carry forward the values of C.
for example:
A B C
1 . 1
1 . 2
1 . 3
1 0 1
1 0 2
1 0 3
This type of output I want. Can anyone please help me ??pro
Can't see that your data example matches your described logic.
Please try to be more precise.
If I have guessed right at what you are looking for, this should do it:
data want;
do until (last.A);
set have;
by A;
if B=. then output;
else retained_B = B;
end;
do until (last.A);
set have;
by A;
if B =. then do;
B = retained_B;
output;
end;
end;
drop retained_B;
run;
If that's not right, you might have to explain what you are looking for.
Good luck.
data Have;
infile cards missover;
input a b c $;
cards;
1 . 1
1 . 2
1 . 3
1 0
2 . 1
2 . 2
2 0
;
proc sql;
create table want1 as
select * from Have where c ne " "
union all
select a.a,a.b,b.c from
(select * from Have where b=0)a left join (select * from Have where c ne " ") b
on a.a = b.a;
quit;
proc sort data =want1 out =want ;
by a b c;
run;
If you load C (and any other variables) into a HASH you can copy a number of variable without having to refer to them directly.
** store the values for C in a small array, then when B =0 put then out;
data one;
infile cards missover;
input A B C;
cards;
1 . 1
1 . 2
1 . 3
1 0 .
2 . 1
2 . 2
2 0 .
3 . 1
3 . 2
3 0 .
;;;;
run;
data two; set one ; by a b;
array hold hold1-hold10;
retain hold1-hold10 .;
if b=. then do;
x+1; hold{x}=c;
output;
end;
if b=0 then do;
do t=1 to x;
c=hold{t}; output; end;
end;
if last.a then do; do t=1 to x; hold(t)=.; end;
x=0;
end; drop hold1-hold10 x t;
proc print; run;
Obs A B C
1 1 . 1
2 1 . 2
3 1 . 3
4 1 0 1
5 1 0 2
6 1 0 3
7 2 . 1
8 2 . 2
9 2 0 1
10 2 0 2
11 3 . 1
12 3 . 2
13 3 0 1
14 3 0 2
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.