Data Value Problem

Reply
New Contributor
Posts: 3

Data Value Problem

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


Super User
Posts: 5,380

Re: Data Value Problem

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

Please try to be more precise.

Data never sleeps
Super User
Posts: 5,353

Re: Data Value Problem

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.

Contributor
Posts: 65

Re: Data Value Problem

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;

Respected Advisor
Posts: 3,786

Re: Data Value Problem

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;


Frequent Contributor
Posts: 91

Re: Data Value Problem

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

 

Ask a Question
Discussion stats
  • 5 replies
  • 4748 views
  • 0 likes
  • 6 in conversation