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

For Example:

vend_net                                 

406,800,802,803,8010
406,800,802,803,8010

 

Pract_net

406,800,802,803
406,800,803,8010

 

Expected ANS:

DIFF

8010

802

So how to find difference value between two column. Can you help me?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

This produces the original answer.  Although you might want to also search in the opposite direction.

data have ;
  length vend_net Pract_net Expected $200;
  input vend_net Pract_net Expected;
cards;
406,800,802,803,8010 406,800,802,803 8010
406,800,802,803,8010 406,800,803,8010 802
;

data want;
  set have;
  length diff $200;
  do i=1 to countw(vend_net,', ');
    if not indexw(Pract_net,scan(vend_net,i,', '),', ') then
      diff=catx(',',scan(vend_net,i,', '))
    ;
  end;
run;
proc print;
run;
Obs          vend_net             Pract_net        Expected    diff    i

 1     406,800,802,803,8010    406,800,802,803       8010      8010    6
 2     406,800,802,803,8010    406,800,803,8010      802       802     6

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User
It is not clear how your data actually looks. Do you have a character variable with values separated by commas? Multiple observations? Multiple variables?
It is also not clear what your logic is that generates that answer.
Spintu
Quartz | Level 8

It is not clear how your data actually looks.

Do you have a character variable with values separated by commas? YES

Multiple observations? YES

Multiple variables? Two Variable
It is also not clear what your logic is that generates that answer. It has to find out different value between two variable in one SAS datasets.Capture1.PNG

Tom
Super User Tom
Super User

This produces the original answer.  Although you might want to also search in the opposite direction.

data have ;
  length vend_net Pract_net Expected $200;
  input vend_net Pract_net Expected;
cards;
406,800,802,803,8010 406,800,802,803 8010
406,800,802,803,8010 406,800,803,8010 802
;

data want;
  set have;
  length diff $200;
  do i=1 to countw(vend_net,', ');
    if not indexw(Pract_net,scan(vend_net,i,', '),', ') then
      diff=catx(',',scan(vend_net,i,', '))
    ;
  end;
run;
proc print;
run;
Obs          vend_net             Pract_net        Expected    diff    i

 1     406,800,802,803,8010    406,800,802,803       8010      8010    6
 2     406,800,802,803,8010    406,800,803,8010      802       802     6
ballardw
Super User

By "difference" do you mean a substring separated by comma that appears in one value but not the other?

What do you want for result if there is no difference? If there are two or more differences?

 

And what do you mean by "regardless of row orders"? Do you mean that you have to compare every value of one variable with every value of the other? You should probably show an example with more than 2 rows of data and the exact expected value for all of the comparisons showing both the starting values for both variables and the desired result. Include at least one example that would have NO difference so we can see what you expect as a result in that case.

Also, are these variables in the same data set or different data sets?

ballardw
Super User

@Spintu wrote:

Capture1.PNG

 

 

Expecting the above example.


Still don't see any definition of what "regardless of row orders" means. It appears that only wanted to compare within a single observation (row).

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 6939 views
  • 0 likes
  • 3 in conversation