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

Hi. I have two variables as following below. 

 

           ID                                Year

US2007025948                     2007

 

What I want to do is that if year (2007) equals to the third to the sixth digit of ID (US2007025948), delete this observation.

 

What code do I need to use? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
if put(year, 4.) = substr(id, 3, 4) then delete;



This assumes year is numeric and ID is character. If it's otherwise you'll need to modify the functions a little bit, but in general that's what you need. It can be modified to be in a WHERE clause, or for SQL or a data step as needed as well.

 


@dapenDaniel wrote:

Hi. I have two variables as following below. 

 

           ID                                Year

US2007025948                     2007

 

What I want to do is that if year (2007) equals to the third to the sixth digit of ID (US2007025948), delete this observation.

 

What code do I need to use? Thanks.


 

View solution in original post

8 REPLIES 8
Reeza
Super User
if put(year, 4.) = substr(id, 3, 4) then delete;



This assumes year is numeric and ID is character. If it's otherwise you'll need to modify the functions a little bit, but in general that's what you need. It can be modified to be in a WHERE clause, or for SQL or a data step as needed as well.

 


@dapenDaniel wrote:

Hi. I have two variables as following below. 

 

           ID                                Year

US2007025948                     2007

 

What I want to do is that if year (2007) equals to the third to the sixth digit of ID (US2007025948), delete this observation.

 

What code do I need to use? Thanks.


 

dapenDaniel
Obsidian | Level 7

Year is also character variable.

Reeza
Super User
Then remove the PUT function and it should work.
dapenDaniel
Obsidian | Level 7

if year=substr(id, 3, 4) then delete;

 

Is it correct?

Reeza
Super User

Does it work?

 


@dapenDaniel wrote:

if year=substr(id, 3, 4) then delete;

 

Is it correct?


 

dapenDaniel
Obsidian | Level 7

there is no error message so I think it works.

Reeza
Super User
Check the output. Just because code runs doesn't mean it's correct. You should always know what you should expect as output and test for that. This will help you avoid mistakes.
dapenDaniel
Obsidian | Level 7

I checked the output. The code works well. Thanks.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 2334 views
  • 2 likes
  • 2 in conversation