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

I need help with the below code snippet.

 

data A dup;
set B;  /* Sorted dataset by id update_dt updt_code*/
by id update_dt updt_code;
if first.id and last.updt_code then output A;
else output dup;
run;

Data B:

id     Update_dt   updt_code
123 14/02/2017   3

123 14/02/2017   3
123 14/02/2017   6

Required Data A:
id     Update_dt   updt_code
123  14/02/2017   3
123  14/02/2017   6

1 ACCEPTED SOLUTION

Accepted Solutions
2 REPLIES 2
Kurt_Bremser
Super User

The most efficient way to achieve your intended (as I guess) result is this IMO:

proc sort
  data=b
  out=a
  nodupkey
  dupout=dup
;
by id update_dt updt_code;
run;
mkeintz
PROC Star

by filtering on "first.id" you can only get at most the first of your three recrods.  But the last.updt_code dummy is not simultaneously true in  your data.  I think you want;

 

data a dup;

  set b;

 by id update_dt updt_code;

 if first.updt_code=1 and last.updt_code=1 then output a;

 else output dup;

run;

 

Remember, if you have multiple BYvars, whenever a given by var is in first. or last. condition, all by vars to its right will also be set to first. or last. condition - even if their values don't change throughout the entire dataset.  By-variable dummies are hierarchical.

--------------------------
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

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 736 views
  • 1 like
  • 3 in conversation