string extraction

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

string extraction

hai all i have a doubt in string extraction.I can use several methods like substr(),index() to extarct 245 from whole string.But i couldn't get any result.Please help me

site

245-abcdefhg

Thanks

krishna


Accepted Solutions
Solution
‎10-11-2014 02:31 PM
Super User
Posts: 19,090

Re: string extraction

Then you haven't provided enough information, works for me just fine, I've added the input to convert to numeric.

This also shows the compress method:

data have;

informat string $25.;

input string;

cards;

245-abcdefhg

;

run;

data want;

  set have;

want=input(scan(string, 1, "-"), 8. );

want2=input(compress(string,, 'kd'), 8.);

type=vtype(want);

type2=vtype(want2);

put want;

put want2;

put type;

put type2;

run;


LOG:


1417  data want;

1418      set have;

1419  want=input(scan(string, 1, "-"), 8. );

1420  want2=input(compress(string,, 'kd'), 8.);

1421  type=vtype(want);

1422  type2=vtype(want2);

1423  put want;

1424  put want2;

1425  put type;

1426  put type2;

1427  run;

245

245

N

N

NOTE: There were 1 observations read from the data set WORK.HAVE.

NOTE: The data set WORK.WANT has 1 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.05 seconds

      cpu time            0.01 seconds




View solution in original post


All Replies
Super User
Posts: 19,090

Re: string extraction

Are all strings in that format? Do you need it as numeric or character?

Try scan

string245=scan(string, 1, "-");

Frequent Contributor
Posts: 89

Re: string extraction

need to get it as numeric

Frequent Contributor
Posts: 89

Re: string extraction

Hai Reeza No result found :smileyplain:

Solution
‎10-11-2014 02:31 PM
Super User
Posts: 19,090

Re: string extraction

Then you haven't provided enough information, works for me just fine, I've added the input to convert to numeric.

This also shows the compress method:

data have;

informat string $25.;

input string;

cards;

245-abcdefhg

;

run;

data want;

  set have;

want=input(scan(string, 1, "-"), 8. );

want2=input(compress(string,, 'kd'), 8.);

type=vtype(want);

type2=vtype(want2);

put want;

put want2;

put type;

put type2;

run;


LOG:


1417  data want;

1418      set have;

1419  want=input(scan(string, 1, "-"), 8. );

1420  want2=input(compress(string,, 'kd'), 8.);

1421  type=vtype(want);

1422  type2=vtype(want2);

1423  put want;

1424  put want2;

1425  put type;

1426  put type2;

1427  run;

245

245

N

N

NOTE: There were 1 observations read from the data set WORK.HAVE.

NOTE: The data set WORK.WANT has 1 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.05 seconds

      cpu time            0.01 seconds




Frequent Contributor
Posts: 89

Re: string extraction

Thanks Reeza

PROC Star
Posts: 7,433

Re: string extraction

data have;

  input site $25.;

  site_number=input(scan(site, 1, "-"),8.);

  cards;

245-abcdefhg

;

Frequent Contributor
Posts: 89

Re: string extraction

thanks Arthur tabachneck

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 327 views
  • 3 likes
  • 3 in conversation