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


From the given dataset, I am trying to filter certain values from the Variable.
Example: There are 2 columns, (ID and Direction) , I need to create a new dataset with Variable Direction having value only North. Eg, My code should pick ID 2, 4 only. I don't need the combination. Only ID's having North Direction.

 

ID Direction
1 South
1 North
1 West
1 East
2 North
3 South
3 North
3 West
4 North
5 South
6 West
6 East

7 South

7 North
data one multi;
length _c $10;
call missing(__c,_f);
do until(last.ID);
set test;
by id direction notsorted;
if _c ne direction then __c+1;
_c=direction;
if direction="NORTH" then _f=1;
end;
do until(last.id);
set TEST;
by id direction notsorted;
if __c=1 then output one;
else output multi

end;
drop _:;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

straight forward sql:

 

data have;
input ID Direction $ ;
cards;
1 South
1 North
1 West
1 East
2 North
3 South
3 North
3 West
4 North
5 South
6 West
6 East
7 South
7 North
;

proc sql;
create table want as
select *
from have 
group by id
having direction='North' and count(distinct direction)=1;
quit;

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

straight forward sql:

 

data have;
input ID Direction $ ;
cards;
1 South
1 North
1 West
1 East
2 North
3 South
3 North
3 West
4 North
5 South
6 West
6 East
7 South
7 North
;

proc sql;
create table want as
select *
from have 
group by id
having direction='North' and count(distinct direction)=1;
quit;
ChrisNZ
Tourmaline | Level 20

Since you have no duplicates, this should work:

data WANT; 
  set HAVE;
  by ID;
  if DIRECTION = 'North' and first.ID and last.ID;
 run;

 

 

 

Kalai2008
Pyrite | Level 9

Thank you

Tom
Super User Tom
Super User

English is a tricky language.

I think that you are asking to find the IDs that only have Direction='North'. 

If your rows are distinct (that is you cannot have the same ID and Direction repeated) then this means you want IDS that have only one observation AND that observation has Direction='North'.

 

data want ;
  set test;
  by id;
  if first.id and last.id and direction='North';
run;

Perhaps you want something like this?

data north(keep=id) south(keep=id) east(keep=id) west=(keep=id) multi;
  set test;
  by id ;
  if not (first.id and last.id) then output multi;
  else if direction='North' then output north;
  else if direction='South' then output south;
  else if direction='East' then output east;
  else if direction='West' then output west;
run;
Kalai2008
Pyrite | Level 9

Perfect...You saved my time.. Thank you ..It worked, simple SQL code.

Kalai2008
Pyrite | Level 9

Thank you and your code worked . My dataset is very huge with 100 variables, over half a million records. I just gave an example, but in real,the column Direction  have many Values. Rows are not distinct, ID's and Direction repeats itself , having duplicates due to other variables.

Reeza
Super User
Why isn't ID=3 selected, it has a North value?
novinosrin
Tourmaline | Level 20

Id has to have only north 🙂

 

You must be tired mam

Reeza
Super User

@novinosrin wrote:

Id has to have only north 🙂

 

You must be tired mam


lol, I actually am. Did a full day of work and then presentations to students and finally eating dinner now!

novinosrin
Tourmaline | Level 20

lol That makes two . Just did a microwave instant noodles (lazy & tired). Gonna sleep in a bit. Good night and sleep tight. Take care!

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
  • 8430 views
  • 9 likes
  • 5 in conversation