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

I've one variable called CRE and it has value like MIN_[NDIP]_2. Now I want to filter only the values [NDIP] using substr function.

 

When I tried with the following, it's not producing the desired result. Any help?

 

substr(cre,5,length(cre)-2)

I want the result only using susbstr function.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@David_Billa wrote:

I've one variable called CRE and it has value like MIN_[NDIP]_2. Now I want to filter only the values [NDIP] using substr function.

 

When I tried with the following, it's not producing the desired result. Any help?

 

substr(cre,5,length(cre)-2)

I want the result only using susbstr function.


Your arithmetic is wrong.  You need also subtract the 4 characters you skipped by starting in column 5.

733  data test;
734    x='MIN_[NDIP]_2    ';
735    len=length(x);
736    want=substr(x,5,len-4-2);
737    put (_all_) (=/);
738  run;

x=MIN_[NDIP]_2 
len=12 
want=[NDIP]

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

scan(cre, 2, "_")

PG
PaigeMiller
Diamond | Level 26

What is the incorrect result that you are getting?

 

Can you provide a small portion of your data set following these instructions? Because obviously when you manually type a character string in a line of text, sometimes the full information is lost about what this character string really is (for example there may be non-printing characters).

 

I want the result only using susbstr function.

I reserve the right to give other answers, to propose simpler methods, especially for the benefit of others who might be reading along, and I think @PGStats has nailed it.

--
Paige Miller
Tom
Super User Tom
Super User

@David_Billa wrote:

I've one variable called CRE and it has value like MIN_[NDIP]_2. Now I want to filter only the values [NDIP] using substr function.

 

When I tried with the following, it's not producing the desired result. Any help?

 

substr(cre,5,length(cre)-2)

I want the result only using susbstr function.


Your arithmetic is wrong.  You need also subtract the 4 characters you skipped by starting in column 5.

733  data test;
734    x='MIN_[NDIP]_2    ';
735    len=length(x);
736    want=substr(x,5,len-4-2);
737    put (_all_) (=/);
738  run;

x=MIN_[NDIP]_2 
len=12 
want=[NDIP]
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
  • 1341 views
  • 6 likes
  • 5 in conversation