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-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2741 views
  • 1 like
  • 2 in conversation