BookmarkSubscribeRSS Feed
Nikos
Fluorite | Level 6

Hi.

I am grouping by ID1 and ID2 but Var1 and Var2 contain defect data.

I would like to correct as shown on the WANT

(Var1 and Var2 values may be ubder different ID1 and ID2)

Thanking you in advance

Nikos

HAVEWANT
ID1ID2Transaction_DateVar1Var2Var1Var2
10020030MAR2012greend_hotgreenhot
10020030MAR2012greenhotgreenhot
30045505JUL2012red coldred cold
30045505JUL2012red_acoldred cold
30045515SEP2012redcoldredcold
30045515SEP2012red cppred cold
10020028JAN2013green_chotgreenhot
10020028JAN2013greenhotgreenhot
30045503JUL2013redcoldredcold
30045505JUL2013red vvvvredcold
30045503JUL2013red_ncoldred cold
30045505JUL2013red coldred cold
8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Could you clarify exactly, with code/test data what you are trying to do.  It looks like the data in var_1 actually has more than one distinct, i.e. "green" is not the same as "green_c" so a grouping would still bring these out, however if you are after distincts on ID1/2 then do a pros sort nodupkey.  Question then is what do you want to get back.  You could also standardize your var1/2 data before grouping.

Nikos
Fluorite | Level 6

Hi,

I am trying to "correct" Var1 and Var2

The "correct" pairs are (red, cold) (green hot) that should replace the "defect" ones

I am trying to solve that with proc sql find the Min and Max for each group by the proc sort the distinct cases and get the "dupout" dataset so I can remerge it back to the original and "correct" the values but the code becomes two long

I wonder whether there is a shortcut to that.

Thank you

I

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This should convert var1/2 to only have the first part of the string, the distinct will then remove any duplicates.

proc sql;

     create table WANT as

     select     distinct

                    ID1,

                    ID2,

                    scan(VAR1,1,"_") as VAR1,

                    scan(VAR2,1,"_") as VAR2

     from        HAVE;

quit;

Nikos
Fluorite | Level 6

Ιt seems that SCAN function does not work since "defect" values may include various kinds of "defection" i.e. vvvv where it should have been cold

Thank you

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So you are going to need to do some data cleaning prior to the step then.  There's no magic "clean my data" button.

nathan_owens
Obsidian | Level 7

Are the distinct values you expect to see only green and red, hot or cold? if so:

if index(var1,"green")>0 then do;

     var1 = 'green'; var2 = 'hot';

end;

else if index(var1,"red")>0then do;

     var1 = 'red; var2 = 'cold';

end;

...

proc sort data=... nodupkey; by id1 id2; run;

ChemoDb
Calcite | Level 5

For clean up, how about:

1) use proc sql to create a list of distinct values for Var1

2) use data step to detect and translate defective values, write the corresponding  desired value to a new var

3) merge the translation table with the original table (by the original var1 values)

The result of merge should give you the original vars and plus your new alternative side by side.  repeat for var2.

zenjamie
Calcite | Level 5

While there is no substitute for proper cleaning of defective data, a shortcut approach would be to take the most common combination of values to be the "correct" set and apply those to your data.  There is no guarantee that this will be correct but the "errors" should be spread across the wide variety of ways it is possible to be wrong so the valid data is anticipated to be the most populous.

In your particular instance it mihgt be more appropriate to hande id1 and var1 separately from id2 and var2 but this might be food for thought:

proc sql noprint;

/*First create distinct combinations of id1, id2, var1 and var2*/

      create table

          work.groups

      as select distinct

            id1, id2, var1, var2, count(*) as counter

      from

          work.have

      group by

          1,2,3,4

      ;

 

/*Then select the most popular combination (on the assumption that this is the one most likely to be correct)*/

      create table

           work.best_group

      as select

           id1, id2, var1, var2

      from

          work.groups

      group by

          1,2

      having

          counter=max(counter)

      ;

 

/*Finally join the original dataset back to the "best" grouping*/

      create table

          work.want

      as select

          h.id1, h.id2, h.Transaction_Date,

          bg.var1, bg.var2

      from

          work.have as h,

          work.best_group as bg

      where

          h.id1 = bg.id1

          and

          h.id2 = bg.id2

      ;

  quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1261 views
  • 0 likes
  • 5 in conversation