BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

Hi All,

I have input like this, Where information for particular id is segregated. 

input
id   name   lastname  number
1  John                         .
1             Deo               . 
1                                 8888

I need Output  like this , where for a particular id , all information should come in single row.


id  name  lastname   number
1  John    Deo            8888

 

Please help me  in solving this.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

See if this works for you. I added an extra ID.

 

data have;
input id name $ lastname $ number;
infile datalines dlm = '|';
datalines;
1|John|   |.    
1|    |Deo|.    
1|    |   |8888 
2|John|   |.    
2|    |Deo|.    
2|    |   |8888 
;

data want;
   update have(obs = 0) have;
   by id;
   if last.ID then output;
run;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Do you have more variables than 3 in your actual data?

Aexor
Lapis Lazuli | Level 10
Yes, posted these as sample only
PeterClemmensen
Tourmaline | Level 20

Ok. Does my code below work for you?

Aexor
Lapis Lazuli | Level 10
let me check 🙂 I will id more ids with different combination and try
PeterClemmensen
Tourmaline | Level 20

See if this works for you. I added an extra ID.

 

data have;
input id name $ lastname $ number;
infile datalines dlm = '|';
datalines;
1|John|   |.    
1|    |Deo|.    
1|    |   |8888 
2|John|   |.    
2|    |Deo|.    
2|    |   |8888 
;

data want;
   update have(obs = 0) have;
   by id;
   if last.ID then output;
run;
Aexor
Lapis Lazuli | Level 10
Thanks. This code worked. Just a small request . Could you please explain how this update statement worked
PeterClemmensen
Tourmaline | Level 20

Sure. This is a weel known technique among SAS programmers to replace missing obs with previous non-missing values. 

 

The key to this trick is the UPDATE Statement. I use have(obs=0) as the master data set to load the relevant variables and no observations into the PDV. Next, I also use have(all observations included) as the transaction data set and update the master data set from this. Also, I have to use an explicit OUTPUT Statement, because by default SAS only updates the first observation in each group and outputs that.

 

As per the Update Statemnet Doc:

 

By default, the UPDATEMODE=MISSINGCHECK option is in effect, so missing values in the transaction data set do not replace existing values in the master data set. If you want missing values in the transaction data set to replace existing values in the master data set, specify the UPDATEMODE=NOMISSINGCHECK option in the UPDATE statement.

Aexor
Lapis Lazuli | Level 10
Thanks a ton!!!!!!!!!!!!
PeterClemmensen
Tourmaline | Level 20

@Aexor  Anytime 🙂

 

Please remember to mark my answer as accepted solution.

Aexor
Lapis Lazuli | Level 10
Yes. Done that. Just one more information .

I have used this code and this also worked fine
data have;
input id name $ lastname $ number;
infile datalines dlm = '|';
datalines;
1|John| |.
1| |Deo|.
1| | |8888
2|John| |.
2| |Deo|.
2| | |8888
;
run;


data want;
update have(obs=0) have;
by id;
/* if last.ID then output; */
run;

Could you please tell me significance of last.id in your code.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 647 views
  • 2 likes
  • 2 in conversation