Dear,
I need to a create a new variable in a dataset from two datasets.
The number in indi (dataset1) variable must match with pid (dataset2) variable value and then the CMINAE should be concatenation of AE||#||PID||TERM.
How to extract the number from indi variable. The indi variable values are different lengths. Please help.
dataset1
ID indi
1 Any EVENT, SPECIFY*-AE1
2 Among EVENT, SPECIFY*-AE2
dataset2
id term pid
1 TOOTHACHE 1
1 HEADACHE 2
2 SINUSITIS 1
2 INFECTION 2
OUTPUT NEEDED.
DATASET3;
ID indi CMINAE
1 Any EVENT, SPECIFY*-AE1 AE#1 TOOTHACHE
2 Any EVENT, SPECIFY*-AE2 AE#2 INFECTION
Below some options:
data have;
infile datalines dsd;
input ID indi $50.;
/* simple way if you can be sure that there is only one number in the string */
want1=input(compress(indi,,'kd'),?? best32.);
/* option 2: use -AE as token */
want2=input(substr(indi,find(indi,'-AE','i')+3),?? best32.);
/* or with a RegEx if the pattern is a bit more complicated*/
retain _rxID;
if _n_=1 then _rxID=prxparse('/-AE(\d+)/oi');
if prxmatch(_rxID, indi) then
do;
want3=input(prxposn(_rxID, 1, indi),?? best32.);
end;
datalines;
1,Any EVENT, SPECIFY*-AE1
2,Among EVENT, SPECIFY*-AE2
2,Among EVENT, SPECIFY*-AE34and something else 77
;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.