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

Hi Everyone,

 

For this question, I'll provide a dummy table to try and explain what I'm looking for. Based on the below table, what I'm looking to do is for TYPE = NEW VEHICLE, check the VIN and if it's a duplicate, flag the oldest MOD.

VINMODTYPEFLAG
3347610NEW VEHICLE1
3347612NEW VEHICLE0
3815916OLD VEHICLE0
391387OLD VEHICLE0
367459NEW VEHICLE1
3674510NEW VEHICLE0

 

I saw a way to delete the last.var or flag by checking a single column for duplicates, but this is a bit more complex than I'm familiar with and requires more logic.

 

Any suggestions would be greatly appreciated.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This seems to work for your example data:

Proc sort data=have;
   by vin type mod;
run;

data want;
   set have;
   by vin type mod;
   if type = 'NEW VEHICLE' and first.Type then flag=1;
   else flag=0;
run;

However, if you have mix of Old and New and mod values for the same vin no promises.

View solution in original post

3 REPLIES 3
ballardw
Super User

I suggest that you provide how we identify  the "oldest" of anything given that data.

 

Second provide data in the form of "what I have" and "what I want".

 

Third, best is to provide the data in the form of a working data step so we can recreate your data and test code.

BlayLay
Obsidian | Level 7
"oldest" would be the lowest MOD (smallest integer) for that particular VIN, if the VIN appears more than once in the dataset.

What I have is the first 3 columns, what I want is the Flag column to be added based on the logic provided above. "0" indicates no flag, 1 indicates a flag.
ballardw
Super User

This seems to work for your example data:

Proc sort data=have;
   by vin type mod;
run;

data want;
   set have;
   by vin type mod;
   if type = 'NEW VEHICLE' and first.Type then flag=1;
   else flag=0;
run;

However, if you have mix of Old and New and mod values for the same vin no promises.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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