Learning SAS? Welcome to the exclusive online community for all SAS learners.

Parsing last 4 char of string

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

Parsing last 4 char of string

Good morning,

I"m working with parsing a string using the scan sub string and index functions.

Trying to do this with the least amount of code.

XXX_XXX_XXXX

Regards,

Jonathan


Accepted Solutions
Solution
‎06-22-2015 01:56 PM
Super User
Super User
Posts: 7,039

Re: Parsing last 4 char of string

The LENGTHN() function is not going to operate very well on the string XX_XXX_0000 unless that is the name of a variable in the data step.  If you want to have it operated on the value of the macro variable then either use quotes to create a string literal that the function can use,  LENGTHN("&test"), or use the function SYMGET() to retrieve the value of the macro variable, SYMGET('test').

The macro variable ID will have nothing to do with the data set variable ID.  If you want to see the value of ID then add a PUT statement to the data step.  If you want to push value to a macro variable then use the CALL SYMPUTX() function.  CALL SYMPUTX('ID',id);

Are you working with macro variables or actual data?

Here are two methods just using macro code.

%let test = XX_XXX_0000;


%let id = %scan(&test,-1,_);

%put &=id;


%let id = %substr(&test,%length(&test)-3);

%put &=id;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Parsing last 4 char of string

In a data step:

Last4 = substr(yourstring,(length(yourstring)-3));

It would be a good idea to specify a length for Last4 before use.

Length Last4 $ 4;

Super User
Super User
Posts: 7,039

Re: Parsing last 4 char of string

What is you definition of LEAST?

If the sections are delimited by _ then use SCAN. 

data _null_;

  str='xxx_xxx_xxxx';

  new=scan(str,-1,'_');

run;

If you want the last 4 characters then use SUBSTRN() with LENGTHN() function.

  new=substrn(str,lengthn(str)-3);

Contributor
Posts: 70

Re: Parsing last 4 char of string

Thanks guys

I gave this a try with "&test" and without double quotes but no return value of 0000 as of yet.

%let test = XX_XXX_0000;

data _null_;

  Length ID $ 4;

ID = substrn(&test,(lengthn(&test)-3));

run;

%put &ID;

Regards,

Jonathan

Solution
‎06-22-2015 01:56 PM
Super User
Super User
Posts: 7,039

Re: Parsing last 4 char of string

The LENGTHN() function is not going to operate very well on the string XX_XXX_0000 unless that is the name of a variable in the data step.  If you want to have it operated on the value of the macro variable then either use quotes to create a string literal that the function can use,  LENGTHN("&test"), or use the function SYMGET() to retrieve the value of the macro variable, SYMGET('test').

The macro variable ID will have nothing to do with the data set variable ID.  If you want to see the value of ID then add a PUT statement to the data step.  If you want to push value to a macro variable then use the CALL SYMPUTX() function.  CALL SYMPUTX('ID',id);

Are you working with macro variables or actual data?

Here are two methods just using macro code.

%let test = XX_XXX_0000;


%let id = %scan(&test,-1,_);

%put &=id;


%let id = %substr(&test,%length(&test)-3);

%put &=id;

Contributor
Posts: 70

Re: Parsing last 4 char of string

Thanks Tom,

Wow do I have much to learn.

Dealing with a macro variable.

This works

%let test = XX_XXX_0000;

%let ID = %scan(&test,-1,_);

%put &ID;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 421 views
  • 3 likes
  • 3 in conversation