- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The second argument of %SCAN must then be an integer representing the position.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.