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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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