SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi 

I am trying to get first name and last using sas macro scan function with modifers like 'b' and 'a' 

it shows error 


%let name1= Gopal Prasad Mandal
%let name2=Binay kumar
%let name3=Shashi Bhushan Mandal

%let last_name=%scan(&name1,-1);  
/*or*/
%let last_name=%scan(&name1,'','b');

%let first_name=%scan(&name1,1);

%let last_name=%scan(&name2,1);
/*or*/
%let last_name=%scan(&name1,'','b');

%let first_name=%scan(&name2,1);

%let last_name=%scan(&name3,-1);
/*or*/

%let last_name=%scan(&name1,'','b');

%let first_name=%scan(&name3,1);

%put  &last_name.;
%put  &first_name;




4 REPLIES 4
japelin
Rhodochrosite | Level 12
First, the first three lines need a semicolon at the end of the line.
The second argument of %SCAN must then be an integer representing the position.
ballardw
Super User

The missing semicolons in the first 3 statements cause many errors before you get to the %scan.

Any time you get error messages: Copy the text from log of the CODE and any notes, warnings or error. On the forum open a text box using the </> icon above the message window and paste the copied text. This is important so we can see the actual code submitted. It is amazing how many people post 'this code had errors' and when we finally get the log find that the actual submitted code was not the code in their question. Also there are many errors that are cause by something that occurs before the place that SAS reports the error, especially when there is a missing semicolon or two.

 

If your error involved "open code recursion" then you have likely placed SAS into an unstable state and you need to 1) save your code, 2) shut down SAS and restart.

 

Please see (Note the first 3 %Let statements now have ;

%let name1= Gopal Prasad Mandal ;
%let name2=Binay kumar          ;
%let name3=Shashi Bhushan Mandal;

%let last_name=%scan(&name1,-1);  
%put &last_name;
/*or*/
%let last_name=%scan(&name1,1,%str( ), b );
%put &last_name;

When you place quotes in most macro functions then the quote becomes part of the VALUE since everything is text to the macro processor. Maybe this will show you what the quotes were attempting to do:

%let weird= Gopal 'Prasad' Mandal ;

%let last_name=%scan(&weird,1,'', b );
%put &last_name;

The quotes in the third parameter place have become the "character list" used for setting delimiters. I include two to avoid the unmatched quote problems.

PaigeMiller
Diamond | Level 26

It's not clear why this task of finding a first name is being done via macro functions rather than a SAS data step and data step functions.

 

Your data will be in a SAS data set, use the proper tool, use SAS data steps and data step functions.

--
Paige Miller
Kurt_Bremser
Super User

Names are data, so they are handled in DATA and procedure steps, not with macro language, which is for code.

If you have a real-life need for splitting strings into words when creating dynamic code, show that.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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