BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ada77
Calcite | Level 5

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

11 REPLIES 11
Reeza
Super User
It looks like you have read in a report instead of raw data,often where cells are combined via merged cells?

Anways, RETAIN is the correct approach, but how many variables do you need to do this with? If it's a lot an array or macro approach may be needed.
Ada77
Calcite | Level 5
I have 8 variable to do that. Do you think make 8 new variable and RETAIN them is suitable here? If I use RETAIN I also should RENAME these new variables afterward
novinosrin
Tourmaline | Level 20

Hello @Ada77   Can you please post a comprehensive and better representative sample ?

Ada77
Calcite | Level 5

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

novinosrin
Tourmaline | Level 20

Please help my understanding.

 

Are you looking to fill(LOCF) missings only the KIND: group of variables ? or for var: group as well?

Ada77
Calcite | Level 5
only KIND: group.
But in actual they do not have same prefix
novinosrin
Tourmaline | Level 20

"But in actual they do not have same prefix" 

 

Oh Well, that's the reason I requested a "representative" sample

Ada77
Calcite | Level 5

I'm sorry. I used the same prefix just hope it will be more easy to understand

novinosrin
Tourmaline | Level 20

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. 

Ksharp
Super User
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;
Ada77
Calcite | Level 5

Thank you so much! It really helps!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 11 replies
  • 3377 views
  • 0 likes
  • 4 in conversation