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.
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.
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.
Year is also character variable.
if year=substr(id, 3, 4) then delete;
Is it correct?
there is no error message so I think it works.
I checked the output. The code works well. Thanks.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.