DATA Step, Macro, Functions and more

merge with index

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

merge with index

Hi,

I am trying to merge with index variables  but when merge finished the output table doesn't have the index table.

here  my A  table has index variables . I want see the same result in TEST table  also(with index variables).

 

data test;

merge a(in=a) b(in=b);

by id;

if a=b then do;

if name ne ' ' then name='XXXX';

end;

if a then output;

run;

 


Accepted Solutions
Solution
‎04-19-2018 09:23 AM
Super User
Posts: 9,919

Re: merge with index

As I already said, use proc datasets to create an index table for a newly created dataset. Index tables are not automatically created.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 9,919

Re: merge with index

If you want to have an index for a newly created dataset, use proc datasets to add it to the dataset. Indexes are not automatically created.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,427

Re: merge with index

Post test data in the form of a datastep and what the output should look like.  At a guess:

if a=b then do;

Does not do what you think it does, you may mean, if a and b, i.e. if record is in both datasets. 

I can't really tell you anything else without seeing some data.

Super User
Posts: 9,919

Re: merge with index


@RW9 wrote:

Post test data in the form of a datastep and what the output should look like.  At a guess:

if a=b then do;

Does not do what you think it does, you may mean, if a and b, i.e. if record is in both datasets. 

I can't really tell you anything else without seeing some data.


The funny thing is that this works, as a = b = 0 can never happen. Although I prefer to use if a and b;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

Re: merge with index

Posted in reply to KurtBremser

yes It is working , but I will change to if a and b. here Id is the index varable

 

Attachment
Attachment
Frequent Contributor
Posts: 76

Re: merge with index

sorry,
here base table is A table and A table is B table
Super User
Super User
Posts: 9,427
Super User
Posts: 9,919

Re: merge with index

[ Edited ]

Running this:

data test;
merge sascomm.base(in=a) sascomm.a(in=b);
by id;
if a=b then do;
  if name ne ' ' then name='XXXX';
end;
if a then output;
run;

proc print data=sascomm.base noobs;
proc print data=sascomm.a noobs;
proc print data=test noobs;
run;

after uploading your datasets, I see no problems at all:

  id    name

   1    qq   
   2    aa   
   3    ss   
   4    ss   
   5    dd   
   6    dff  
   7    vff  
   8    gggg 
   9    ggggg
  10    rrr  
  11    sss  
  12    ssss 
  13    dddd 
  14    sssss
                

       id

        2
        5
        6
        7
       10
                

  id    name

   1    qq   
   2    XXXX 
   3    ss   
   4    ss   
   5    XXXX 
   6    XXXX 
   7    XXXX 
   8    gggg 
   9    ggggg
  10    XXXX 
  11    sss  
  12    ssss 
  13    dddd 
  14    sssss

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

Re: merge with index

Posted in reply to KurtBremser
yes, sascomm.base table is an index table on ID.(didn't upload index table).
when we perform merge test table doesn't create an index table.
Frequent Contributor
Posts: 76

Re: merge with index

Posted in reply to KurtBremser

 

data sascomm.base(index=(id));

set sascomm.base;

run;

data test;
merge sascomm.base(in=a) sascomm.a(in=b);
by id;
if a=b then do;
  if name ne ' ' then name='XXXX';
end;
if a then output;
run;

 

when we perform this. test table is not creating the index table . I want to get the index table also.

Solution
‎04-19-2018 09:23 AM
Super User
Posts: 9,919

Re: merge with index

As I already said, use proc datasets to create an index table for a newly created dataset. Index tables are not automatically created.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 134 views
  • 0 likes
  • 3 in conversation