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

Hello:

 

I have the following data.  I would like to remove all of the '_' and assign what the number in the string at the end.  Please help.  Thanks.

 

data have;

input Names$100.;

cards;

lab_den_pcr_perf_1

mhh_cyto_tests_1__cfdna

nad_cert_30

nad_imag_find2___abn_cort_gyr

;

run;

 

The result I am looking for is

labdenpcrperf1

mhhcytotestscfdna1

nadcert30

nadimagfindabncortgyr2

1 ACCEPTED SOLUTION

Accepted Solutions
jdwaterman91
Obsidian | Level 7

@RW9

 

Specifying the k-modifier in the compress function keeps the "_" instead of removing it. 

 

I also think he wants the numbers at the end of the string instead of the beginning.

 

So modifying your code, I think this will work:

 

data want2;
  set have;
  names=cats(compress(names,"_","d"), compress(names,"_","a"));
run;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

You can use the COMPRESS function to remove certain characters from a string. Not sure what you mean by "assign what the number in the string at the end". Do you want to have the number of occurences of "_" at the end of each string?

Patrick
Opal | Level 21

@ybz12003

Providing a "desired result" would help us to better understand what you're after.

 

The following code will populate a numeric variable with a number if digits only are found as the last element of your source string (with underscore separating the "elements").

data have;
  input Names :$100.;
  cards;
lab_den_pcr_perf_1
mhh_cyto_tests_1__cfdna
nad_cert_30
nad_imag_find2___abn_cort_gyr
;
run;

data want;
  set have;
  no_at_end_of_str=input(scan(names,-1,'_'),?? best32.);
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

data want;
  set have;
  names=cats(compress(names,"_","ka"),compress(names,"_","kd"));
run;
jdwaterman91
Obsidian | Level 7

@RW9

 

Specifying the k-modifier in the compress function keeps the "_" instead of removing it. 

 

I also think he wants the numbers at the end of the string instead of the beginning.

 

So modifying your code, I think this will work:

 

data want2;
  set have;
  names=cats(compress(names,"_","d"), compress(names,"_","a"));
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Actually my code is fine, just remove the _, whiich I added in haste:

data want;
  set have;
  names=cats(compress(names,"","ka"),compress(names,"","kd"));
run;
Astounding
PROC Star

Here's a way:

 

data want;

set have;

prefix = compress(names, '_0123456789');

suffix = compress(names, , 'kd' );

names = strip(prefix) || suffix;

drop prefix suffix;

run;

 

One caution:  If there are multiple sets of digits, all of them get combined and put at the end.  For example:

 

abc1_xyz2_def becomes abcxyzdef12

DoumbiaS
Quartz | Level 8

Like this ?

 

data want;

set have;

n_position= anydigit(names,1);

number= compress(substr(names,n_position),,"kd");

names_want= cats(compress(names,'_',"d"),number);

keep names_want;

run;

kiranv_
Rhodochrosite | Level 12

something like this in regex

data want;
set have;
name = prxchange('s/_|([^\d]+$)//', -1, names);
run;
ybz12003
Rhodochrosite | Level 12

I start to understand what the prxchange means.

Patrick
Opal | Level 21

@ybz12003

The following code returns the desired output as you've posted.

Based on your narrative I'm not sure if this really is what you're after. If not then please post some additional sample data where it's not working for you.

data have;
  input Names :$100.;
  cards;
lab_den_pcr_perf_1
mhh_cyto_tests_1__cfdna
nad_cert_30
nad_imag_find2___abn_cort_gyr
;
run;

data want;
  set have;
  length names_want $100;
  names_want=cats(compress(Names,'_','d'),compress(Names,,'kd'));
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 4609 views
  • 3 likes
  • 8 in conversation