BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KrishnaChandra
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

7 REPLIES 7
Reeza
Super User

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

Try scan

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

KrishnaChandra
Calcite | Level 5

need to get it as numeric

KrishnaChandra
Calcite | Level 5

Hai Reeza No result found :smileyplain:

Reeza
Super User

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




KrishnaChandra
Calcite | Level 5

Thanks Reeza

art297
Opal | Level 21

data have;

  input site $25.;

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

  cards;

245-abcdefhg

;

KrishnaChandra
Calcite | Level 5

thanks Arthur tabachneck

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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