DATA Step, Macro, Functions and more

How to extract a number from a character string

Reply
Super Contributor
Posts: 272

How to extract a number from a character string

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

 

Respected Advisor
Posts: 3,892

Re: How to extract a number from a character string

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 1 reply
  • 192 views
  • 1 like
  • 2 in conversation