Extract character from string (scan automatisation)

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Extract character from string (scan automatisation)

Hellow friends,

data

 

data test;
length usercation $ 200;
input usercation $ ;

cards;

PDF_STUDY_11225_15/12/2016
PDF_45896_12/05/2017_HTT
PDF_STUDY_CURVE
;
run;

 

I want to create a new variable which contains just the first carcaterial part of the variable usercation before the '_'  

for example: PDF_STUDY_11225_15/12/2016 ->PDF_STUDY

                    PDF_45896_12/05/2017_HTT       ->PDF

                     PDF_STUDY_CURVE                 -> PDF_STUDY_CURVE

 

In fact I would like automated processing for many lines.

Thank you very much for your help!


Accepted Solutions
Solution
‎08-24-2017 11:28 AM
Trusted Advisor
Posts: 1,137

Re: Extract character from string (scan automatisation)

Please try

 

data test;
length usercation $ 200;
input usercation $ ;

cards;

PDF_STUDY_11225_15/12/2016
PDF_45896_12/05/2017_HTT
PDF_STUDY_CURVE
;
run;

data want;
set test;
if prxmatch('/_\d/',usercation)>0 then new=substr(usercation,1,prxmatch('/_\d/',usercation));
else new=usercation;
run;
Thanks,
Jag

View solution in original post


All Replies
Solution
‎08-24-2017 11:28 AM
Trusted Advisor
Posts: 1,137

Re: Extract character from string (scan automatisation)

Please try

 

data test;
length usercation $ 200;
input usercation $ ;

cards;

PDF_STUDY_11225_15/12/2016
PDF_45896_12/05/2017_HTT
PDF_STUDY_CURVE
;
run;

data want;
set test;
if prxmatch('/_\d/',usercation)>0 then new=substr(usercation,1,prxmatch('/_\d/',usercation));
else new=usercation;
run;
Thanks,
Jag
New Contributor
Posts: 3

Re: Extract character from string (scan automatisation)

Posted in reply to Jagadishkatam

Thank you very much!

it works very well!

PROC Star
Posts: 7,543

Re: Extract character from string (scan automatisation)

Minor correction to @Jagadishkatam's suggested code:

data want;
  set test;
  if prxmatch('/_\d/',usercation)>0 then new=substr(usercation,1,prxmatch('/_\d/',usercation)-1);
  else new=usercation;
run;

Art, CEO, AnalystFinder.com

 

New Contributor
Posts: 3

Re: Extract character from string (scan automatisation)

Thank you very much for correction!

it works very well!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 132 views
  • 1 like
  • 3 in conversation