BookmarkSubscribeRSS Feed
lpy0521
Fluorite | Level 6

Hi, there

 

my data looks like this:
ID   var1  var2
111 aaa   100
111 aaa   101
111          102

111          
222 bbb  101
222 bbb  102
222         103

Just curious how can I fill the null var1 with previous row's value ony if var2 is not null and the desired data looks like:  

 

want 
ID   var1 var2
111 aaa  100
111 aaa  101
111 aaa  102

111
222 bbb  101
222 bbb  102
222 bbb  102

 

 

Thanks in advance!

6 REPLIES 6
novinosrin
Tourmaline | Level 20

data have;
input ID   var1 $ var2 ;
cards;
111 aaa   100
111 aaa   101
111   .       102
111    .       .
222 bbb  101
222 bbb  102
222    .     103
;

data want;
set have;
by id;
k=lag(var1);
if not first.id and missing(var1) and k>' ' and var2 then var1=k;
drop k;
run;
Ksharp
Super User

data have;
input ID   var1 $ var2 ;
cards;
111 aaa   100
111 aaa   101
111   .       102
111    .       .
222 bbb  101
222 bbb  102
222    .     103
;

data want;
 set have;
 length new_var1 $ 80;
 retain new_var1 new_var2;
 by id;
 if first.id then call missing(new_var1,new_var2);
 if not missing(var1) then new_var1=var1;
 if not missing(var2) then new_var2=var2;
 if cmiss(new_var1,new_var2)=2 then call missing(new_var1,new_var2);
run;
mkeintz
PROC Star

What if you have consecutive instances of missing var1  but non-missing var2?   I.e. what if you have

data have;
input ID   var1 $ var2 ;
cards;
111 aaa   100
111 aaa   101
111   .       102
111   .       103
111    .       .
222 bbb  101
222 bbb  102
222    .     103
;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
lpy0521
Fluorite | Level 6

I will only copy var1 when var 2 is not missing. missing var 1 is not irrelevant in this case, thanks!

Allaluiah
Quartz | Level 8

What have you tried? Can you show your sincere attempt? I have noticed you haven't showed either in any of your threads and I think that's unfair

ShiroAmada
Lapis Lazuli | Level 10

Try this.

data want;
 set sample;
var1=ifc ((var1="" and var2>0),lag(var1),var1);
run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 6 replies
  • 1466 views
  • 0 likes
  • 6 in conversation