BookmarkSubscribeRSS Feed
SJN
Fluorite | Level 6 SJN
Fluorite | Level 6

Hi All,

 

Looking for logic help

My sample data

 

Number Code
1           a1
1           a2
2           b
2           b
3           a1
3           c

 

Here Number 1 is initially in Code a1 and converted to a2 the same Number 3 converted yo a1 to c

My requirement is looking for Number 2 (i,e. customers who didnt changes the plancode)

 

Regards,

SJ

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

And what do you want to do then? Do you want to have all the records for Number=2 in a data set or?

 

If so, do something like this

 

data have;
input Number Code $;
datalines;
1 a1
1 a2
2 b
2 b
3 a1
3 c
;

proc sql;
   create table want as
   select * from have
   group by Number
   having count(distinct Code)=1;
quit;
SJN
Fluorite | Level 6 SJN
Fluorite | Level 6

Thank you for the code your code worked for me I used other filter conditions as well. There are some data issues I'm working on it.

PeterClemmensen
Tourmaline | Level 20

Ok. Let me know if there is anything else I can do.

 

Otherwise, remember to mark the thread as accepted 🙂

 

Regards.

SJN
Fluorite | Level 6 SJN
Fluorite | Level 6

Sure 🙂

mkeintz
PROC Star

If you values are grouped by NUMBER, then this data step is efficient:

 

data want;
  do ncodes=1 by 1 until (last.number);
    do until (last.code);
      set have;
      by number code notsorted;
    end;
  end;
  if ncodes=1;
run;

 

The BY statement uses the NOTSORTED option because while the data is probably sorted by number, it may not be sorted by code-within-number.  So this program just takes consecutive instances of a given number, and counts the number of changes to code within that number.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SJN
Fluorite | Level 6 SJN
Fluorite | Level 6

Just want understand what happens when we write the do loop before set statement.

 

The same loop if I'm writing after the set statement it's making an infinite loop. Can you explain me please

ruchi11dec
Obsidian | Level 7

if looking for base sas solution :

 

data want(keep = number code);
set have;
by number;
retain code_new;
if first.number then code_new = code;
else do; if code = code_new then change = "n"; end;
if change = 'n';
run;

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!

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
  • 7 replies
  • 1104 views
  • 3 likes
  • 4 in conversation