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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.