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

Hello,

how can I cut off part of sting like in below example:

VAR
USE_01
USE_12
USE_03

I want to keep only numbers without "0" -> so result should be 1,12,3

How can I achieve this?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    part_of_string = input(scan(var,2,'_'),3.);
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
tdswart
Fluorite | Level 6

Hello,

 

You could fix this with a data step and tranwrd. Below you can see an example (with example dataset) that gives the result you want in 2 steps. 

 

data work.sastest;
input VAR1 $char6.;
format VAR1 char6.;
datalines ;
USE_01
USE_12
USE_03
;
run;


data work.sastest2;
set work.sastest;
VAR2= tranwrd(VAR1,'USE_','');
VAR3= tranwrd(VAR2,'0','');
run;

 

Hope this helps you!

PaigeMiller
Diamond | Level 26

I want to keep only numbers without "0" -> so result should be 1,12,3

It's really hard to generalize from such a small example. So let me ask a few questions

 

  • Are the first four characters always USE_ or can they be something else?
  • Is there always an underscore?
  • What would you do about USE_10, the above statement seems to indicate you don't want it because it has a zero

 

 

--
Paige Miller
PatrykSAS
Obsidian | Level 7
Hi PaigeMiller,
Yes its always pattern USE_ with underscore
If its USE_10 I'd keep number 10
PaigeMiller
Diamond | Level 26
data want;
    set have;
    part_of_string = input(scan(var,2,'_'),3.);
run;
--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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