BookmarkSubscribeRSS Feed
RahulBandgar
Calcite | Level 5

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


5 REPLIES 5
LinusH
Tourmaline | Level 20

Can't see that your data example matches your described logic.

Please try to be more precise.

Data never sleeps
Astounding
PROC Star

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.

MadhuKorni
Quartz | Level 8

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;

data_null__
Jade | Level 19

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.

data abc;
  infile cards missover;
 
input A      B      C;
  set sashelp.class point=_n_;
  cards;
1      .          1
1      .          2   
1      .          3
1      0           
2      .          1
2        .          2
2        0       
3        .          1
3        .          2
3        0       
;;;;
  run;

data abclocf;
  if 0 then set abc(drop=a b);
  length _name_ $32 _obs_ 8;
 
if _n_ eq 1 then do;
     
declare hash h(ordered:'Y');
      h.definekey('_OBS_');
      do while(1);
        call vnext(_name_);
        putlog 'NOTE: ' _name_=;
        if missing(_name_) then leave;
        if lowcase(_name_) eq '_name_' then leave;
        h.definedata(_name_);
       
end;
      h.definedone();
     
end;
  _obs_ =
0;
 
do until(last.a);
      set abc;
      by a;
      if b eq . then do;
       
output;
        _obs_ +
1;
        _rc_ = h.add();
       
end;
     
else if last.a and b eq 0 then do _obs_ = 1 to _obs_;
        _rc_=h.find();
       
output;
       
end;
     
end;
  _rc_=h.clear();
 
drop _obs_ _rc_ _name_;
  run;
proc print;
 
run;


Jim_G
Pyrite | Level 9

** 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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 6099 views
  • 0 likes
  • 6 in conversation