BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11
data find;
input diseaseName $160. code $45.;
datalines;
cervical cancer 123.z
HepA 9970
;
run;

Hello team,

I want to find a string within another string, if it is found, then grab the correspondent values from another variable.

For example, if value of cervical if found in cervical cancer, then grap the character 123.z.

Any help is greatly appreciated.

 

Regards,

blue & blue

Blue Blue
7 REPLIES 7
japelin
Rhodochrosite | Level 12

First of all, the data step you presented does not work correctly, please correct it to the correct one.

If you want to find a string, you can use the find function.

 

  if(find(diseaseName,'cervical','I'))then newcode=code;
GN0001
Barite | Level 11

Hello team,

This doesn't bring the value from other variable when we find one string within another string.

Regards,

blue&blue

Blue Blue
japelin
Rhodochrosite | Level 12
First of all, can you tell me exactly what kind of data you want to derive from what kind of data?
Not inaccurate data steps.
andreas_lds
Jade | Level 19

Please explain what "grab the correspondent values from another variable" actually means by showing the expected output.

GN0001
Barite | Level 11

Hello team,

It means variable A from first dataset looks at the field A second dataset and brings a value from anther field.

For Example:

If cervical cancer from table A is found in string of column A of second dataset (cervical cancer screening), then bring the code which exists in a different column in second dataset in the result of the query.

 

cervical cancer is a substring of cervical cancer screening, then bring code that is in the observation of cervical cancer screening, but in a different column.

Hope this makes sense.

 

Regards,

blue blue

 

 

Blue Blue
ballardw
Super User

When posting code to illustrate data you should run the code before posting it.

When I run your data step this is my log:

1    data find;
2    input diseaseName $160. code $45.;
3    datalines;

NOTE: LOST CARD.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
6          ;
diseaseName=HepA 9970 code=  _ERROR_=1 _N_=1
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.

What has happened is that you are unconditionally attempting to read 160 characters into the first variable which is longer than the data line you provide. If you must include example data with spaces in the values it is a good idea to use delimited text to simplify coding and correctly read the spaces.

data find;
infile datalines dlm=',' ;
input diseaseName :$160. code :$45.;
datalines;
cervical cancer,123.z
HepA, 9970
;
GN0001
Barite | Level 11

Hello Tom,

I couldn't understand anything of this:

"What has happened is that you are unconditionally attempting to read 160 characters into the first variable which is longer than the data line you provide. If you must include example data with spaces in the values it is a good idea to use delimited text to simplify coding and correctly read the spaces."

 

Obviously, I want my first variable to have more space if I need it so, I can provide max lenght.

f you must include example data with spaces in the values it is a good idea to use delimited text to simplify coding and correctly read the spaces." 

Are you saying to put my text in single quotes if the content of a variable has space?

Regards,

BlueBlue

 

Blue Blue

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1352 views
  • 2 likes
  • 4 in conversation