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

I have a dataset with a character variable called "oldvar" that contains a string of numeric values such as "4 6 6 4 5". I would like to create a new variable that has one of the "4" values removed. For example, I would like my new variable to be "6 6 4 5" with the first 4 deleted or alternatively the new variable could be "4 6 6 5". Either one would work for me. I've tried using a combination of the substr and index functions but none have produced what I'm looking for.

 

Example of values that "oldvar" takes on: 

4 5 4 5

4 6 4

6 6 4 5

3 4 3

 

Thank you in advance for any help! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11

Here's another approach just for options.

 

data have;
	input oldvar $20.;
datalines;
4 5 4 5
4 6 4
6 6 4 5
3 4 3
;
run;

data want;
	set have;
	newvar= prxchange('s/4 //', 1, oldvar); 
run;

This yields

The SAS System  
   
oldvar newvar
4 5 4 5 5 4 5
4 6 4 6 4
6 6 4 5 6 6 5
3 4 3 3 3

 

 

The documentation:

prxchange('s/world/planet/', 1, 'Hello world!'); 

 

where

s

specifies the metacharacter for substitution.

world

specifies the regular expression.

planet

specifies the replacement value for world.

1

specifies that the search ends when one match is found.

Hello world!

specifies the source string to be searched.

 

In this case: s, 4 and a space, nothing (//), 1, and the old variable.

View solution in original post

8 REPLIES 8
Reeza
Super User

What's the rules?
How do you know what to delete and what to keep?

 


@Jaime2 wrote:

I have a dataset with a character variable called "oldvar" that contains a string of numeric values such as "4 6 6 4 5". I would like to create a new variable that has one of the "4" values removed. For example, I would like my new variable to be "6 6 4 5" with the first 4 deleted or alternatively the new variable could be "4 6 6 5". Either one would work for me. I've tried using a combination of the substr and index functions but none have produced what I'm looking for.

 

Example of values that "oldvar" takes on: 

4 5 4 5

4 6 4

6 6 4 5

3 4 3

 

Thank you in advance for any help! 

 


 

Jaime2
Obsidian | Level 7

I'm creating control cards for another program. The rule is to literally "delete one "4" from the string of values." There is always at least one "4" in the variable, sometimes there are 2 or 3 of them. Thanks for asking. 

HB
Barite | Level 11 HB
Barite | Level 11

So what is the final desired product from your data set?

 

Is this what you want?

 

have want
4 5 4 5 5 4 5
   
4 6 4 6 4
   
6 6 4 5 6 6 5
   
3 4 3 3 3
Jaime2
Obsidian | Level 7

Yes, this is exactly what I want to achieve. 

Astounding
PROC Star

One approach:

 

data want;

set have;

found4 = indexw(oldvar, '4');

select (found4);

   when (0);

   when (1) oldvar = substr(oldvar, 3);

   otherwise oldvar = catx(' ', substr(oldvar, 1, found4 - 1), substr(oldvar, found4 + 1)) ;

end;

drop found4;

run;

 

I'm sure there are ways to do this with parsing functions, but I'm not as familiar with them.

 

************ EDITED:

 

See the @HB solution below.  There's a good reason to learn parsing functions.

HB
Barite | Level 11 HB
Barite | Level 11

Here's another approach just for options.

 

data have;
	input oldvar $20.;
datalines;
4 5 4 5
4 6 4
6 6 4 5
3 4 3
;
run;

data want;
	set have;
	newvar= prxchange('s/4 //', 1, oldvar); 
run;

This yields

The SAS System  
   
oldvar newvar
4 5 4 5 5 4 5
4 6 4 6 4
6 6 4 5 6 6 5
3 4 3 3 3

 

 

The documentation:

prxchange('s/world/planet/', 1, 'Hello world!'); 

 

where

s

specifies the metacharacter for substitution.

world

specifies the regular expression.

planet

specifies the replacement value for world.

1

specifies that the search ends when one match is found.

Hello world!

specifies the source string to be searched.

 

In this case: s, 4 and a space, nothing (//), 1, and the old variable.

Jaime2
Obsidian | Level 7

Your solution worked - thank you! 

Jaime2
Obsidian | Level 7

Thank you very much! Your solution also worked just fine!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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