I am new to this forum. But I have read many answers to questions that I had in SAS coding as well on this website. I have run into a problem with SAS coding at work that I hope somebody can help.
I am trying to extract a numeric substring from a text string. The numeric string is always before words like "YR" or "YEAR". Sometimes there is a space between the numeric substring and "YR" or "YEAR". Both the numeric substring and the text string vary in length from obs to obs. Here is an example of what it looks like: Screenshot of SAS dataset
The number right before "YR" or "YEAR" is the numeric string I want to extract. I have tried to use find fn to locate where the "YR" or "YEAR" is and then use substrn to extract the surrounding string. Then compress the characters. But the result is not ideal as sometimes it pulls the number in the first part of the string and sometimes it doesn't pull in the whole number (e.g. 4.75). Here is the code that I have used:
if find(deal_type_oss, "YR","i") ne 0
then term=compress(substrn(deal_type_oss, find(deal_type_oss, "YR","i")-4,6),"","a");
if find(deal_type_oss,"Year","i") ne 0
then term=compress(substrn(deal_type_oss, find(deal_type_oss, "Year","i")-4,6),"","a");
Here is the result of this code: Results of the code
Thank you in advance!
Tao