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

In the following program I want to extract the word from last.Desired result is MIN, MAX, AVG

I'm looking for two Solutions here. One with using only substr and other with combination of Scan and substr or with any function.

Any help?

 

data test;
length CRE $50;
input CRE $;
datalines;
[NDID]#MIN2
[NDID_TST]#MAX2
[NDID_PROD]#AVG ; run;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data test;
length CRE $50;
input CRE $;
datalines;
[NDID]#MIN2
[NDID_TST]#MAX2
[NDID_PROD]#AVG
;
run;

data want;
  set test;
  want=scan(cre,-1,,'ka');
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
want = scan(cre,-1,'#');

To do this with SUBSTR, you'd probably need to use the FIND function, find the location of the # character, and then SUBSTR. SCAN is so much easier.

--
Paige Miller
Patrick
Opal | Level 21

@David_Billa Below for you to pick and chose

data have;
  length CRE $50;
  input CRE $;
  datalines;
"[NDID]#MIN2"
"[NDID_TST]#MAX2"
"[NDID_PROD]#AVG"
"[NDID_PROD]#XXX"
;

data want;
  set have;
  length word1_l4 $4 word1_l3 word2-word4 $3;
  word1_l4=scan(cre,-2,'#"');
  word1_l3=scan(cre,-2,'#"');
  word2=compress(scan(cre,-2,'#"'),,'d');
  word3=substrn(scan(cre,-2,'#"'),1,3);
  word4=substrn(cre,find(cre,'#')+1,3);
run;
David_Billa
Rhodochrosite | Level 12

@Patrick  what is the meaning of this Argument '#"' in your code?

PaigeMiller
Diamond | Level 26

@David_Billa wrote:

In the following program I want to extract the word from last.Desired result is MIN, MAX, AVG

I'm looking for two Solutions here. One with using only substr and other with combination of Scan and substr or with any function.

Any help?

 

data test;
length CRE $50;
input CRE $;
datalines;
"[NDID]#MIN2"
"[NDID_TST]#MAX2"
"[NDID_PROD]#AVG" ; run;

Looking at the above code, is it truly trying to say that the first and last character of the values of CRE strings are double quotes? Or are the double quotes a mistake, and you really mean

 

data test;
length CRE $50;
input CRE $;
datalines;
[NDID]#MIN2
[NDID_TST]#MAX2
[NDID_PROD]#AVG
;
run;
--
Paige Miller
David_Billa
Rhodochrosite | Level 12

@PaigeMiller @Patrick sorry, double quotes a mistake, Ignore the double quotes in the provided data.

ern_23
Obsidian | Level 7

Assuming you have only one # in the column and extract the info without quotation marks, below code should work out.

 

DATA TEST_OUTPUT;
SET TEST;

DESIREDCOLUMN = COMPRESS(SUBSTR(CRE,INDEX(CRE,"#")+1),"""");
RUN;

Ksharp
Super User
data test;
length CRE $50;
input CRE $;
datalines;
[NDID]#MIN2
[NDID_TST]#MAX2
[NDID_PROD]#AVG
;
run;

data want;
  set test;
  want=scan(cre,-1,,'ka');
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1193 views
  • 6 likes
  • 5 in conversation