Suppose I have a dataset:
--Have--
KIND KIND_CD var1 var2...
A 1 1
A . 2
A 1 3
. 1 4
. 1 5
All observations belongs to same KIND, but for some reason some values are missing. The dataset I want is:
--want--
KIND KIND_CD var1 var2...
A 1 1
A 1 2
A 1 3
A 1 4
A 1 5
I tried to create KIND_1 and KIND_CD_1 to retain the non-missing value. But since I have lots of 'KIND' variables, this may not be a good method.
data have;
input KIND $ KIND_CD var1;
id=1;
cards;
A 1 1
A . 2
A 1 3
. 1 4
. 1 5
;
run;
data want;
update have(obs=0) have;
by id;
output;
drop id;
run;
Hello @Ada77 Can you please post a comprehensive and better representative sample ?
Okay. Actually this is a dataset after merge. Suppose all datas in this dataset should belong to KIND A, but some of them do not match KIND A. I used a indicator to show if they are matched. But I also want to show 'the kind that these obs are supposed to belong to', cause I may stack datasets of different kinds.
--Have--
KIND KIND_CD var1 var2... match
A 1 1 1
A 1 2 1
A 1 3 1
. . 4 0
. . 5 0
Hope this will be more clear
Please help my understanding.
Are you looking to fill(LOCF) missings only the KIND: group of variables ? or for var: group as well?
"But in actual they do not have same prefix"
Oh Well, that's the reason I requested a "representative" sample
I'm sorry. I used the same prefix just hope it will be more easy to understand
If you could provide us a clean sample(mock is fine) with the correct set of var names assuming kind group belong to the same type, i can give it a shot.
data have;
input KIND $ KIND_CD var1;
id=1;
cards;
A 1 1
A . 2
A 1 3
. 1 4
. 1 5
;
run;
data want;
update have(obs=0) have;
by id;
output;
drop id;
run;
Thank you so much! It really helps!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.