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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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