Hello Community,
I've got a pretty advanced task and I have absolutely no idea how to do this. So feel free to help 🙂
I have a variable "medicinalproduct", which contains a lot of different informations. One information is the API Code (as you can see in the picture). The Code itself has always the same sequence. It always starts with a letter, then its 2 numbers, 2 letters, 2 numbers(for example in the first line, the API Code is B03XA01. I need to extract this code from the variable and have to create a new variable called API.
How can I do this? I have absolutely no idea. Does anybody know?Thank you soo much for your help. Cheers, Mary.
Please post data in the form of a data step and not a picture.
This is a fit for PRXMATCH I think.
With
data textfield;
input medicinalproduct $100. ;
datalines;
upon a 23234night both dampB03XA01 and dreary GX334&$% when I was so4789 very weary%%$
there came a raven L01XA03 knocking at my door
it was the bV08AB09Rest of time and the worst of ravens
;
data apicodes;
set textfield;
startpos = prxmatch('[[A-Z]{1}[0-9]{2}[A-Z]{2}[0-9]{2}]', medicinalproduct);
apicode = substr(medicinalproduct, startpos, 7);
run;
I can get
The SAS System | ||
medicinalproduct | startpos | apicode |
upon a 23234night both dampB03XA01 and dreary GX334&$% when I was so4789 very weary%%$ | 28 | B03XA01 |
there came a raven L01XA03 knocking at my door | 20 | L01XA03 |
it was the bV08AB09Rest of time and the worst of ravens | 13 | V08AB09 |
Perhaps that is helpful.
I am terrible at regex development so somebody else here can probably make you a much terser expression.
Hello Community,
I've got a pretty advanced task and I have absolutely no idea how to do this. So feel free to help 🙂
I have a variable "medicinalproduct", which contains a lot of different informations. One information is the API Code (as you can see in the picture). The Code itself has always the same sequence. It always starts with a letter, then its 2 numbers, 2 letters, 2 numbers(for example in the first line, the API Code is B03XA01. I need to extract this code from the variable and have to create a new variable called API.
How can I do this? I have absolutely no idea. Does anybody know?Thank you soo much for your help.
Cheers, Mary.
Please post data in the form of a data step and not a picture.
This is a fit for PRXMATCH I think.
With
data textfield;
input medicinalproduct $100. ;
datalines;
upon a 23234night both dampB03XA01 and dreary GX334&$% when I was so4789 very weary%%$
there came a raven L01XA03 knocking at my door
it was the bV08AB09Rest of time and the worst of ravens
;
data apicodes;
set textfield;
startpos = prxmatch('[[A-Z]{1}[0-9]{2}[A-Z]{2}[0-9]{2}]', medicinalproduct);
apicode = substr(medicinalproduct, startpos, 7);
run;
I can get
The SAS System | ||
medicinalproduct | startpos | apicode |
upon a 23234night both dampB03XA01 and dreary GX334&$% when I was so4789 very weary%%$ | 28 | B03XA01 |
there came a raven L01XA03 knocking at my door | 20 | L01XA03 |
it was the bV08AB09Rest of time and the worst of ravens | 13 | V08AB09 |
Perhaps that is helpful.
I am terrible at regex development so somebody else here can probably make you a much terser expression.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.