Help using Base SAS procedures

Displaying a letter from the variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Displaying a letter from the variable

[ Edited ]

Hello here below i have imported a excel sheet which contains certain string i.e  

below is the code i tried can u'll help to correct this code to get my desired output or i use a macro? help! is appreciated....

thanks...

 

data test;
set work.temp;
fullpath=scan(fullpath,-5);
run;

 

 


Accepted Solutions
Solution
‎07-26-2016 12:45 AM
Super User
Posts: 6,963

Re: Displaying a letter from the particular address

In case you want to use a macro like a function, you need to define it differently. But I'd rather use a user-defined data step function instead:


%macro extract_letter(instring);
substr(scan(&instring,-2,'\'),1,1) 
%mend;

proc fcmp outlib=work.funcs.test;
function extract_letter(instring $) $;
  return (substr(scan(instring,-2,'\'),1,1));
endsub;
run;

options cmplib=work.funcs;

data want;
fullpath = 'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ';
result1 = extract_letter(fullpath);
result2 = extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
result3 = %extract_letter(fullpath);
result4 = %extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
run;

Note that the results in the dataset from the function will have the desired length of 1, while the results from the macro invocation will default to length 200.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,963

Re: Displaying a letter from the particular address

Try

substr(scan(fullpath,-2,'\'),1,1)
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Displaying a letter from the particular address

but how would i incorporate this in a macro?
Super User
Posts: 6,963

Re: Displaying a letter from the particular address


RTelang wrote:
but how would i incorporate this in a macro?

Why would you want to do that?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Displaying a letter from the particular address

I have multiple strings like this in my excel sheet so i would just run the macro & display the letter so i asked abt the macro....
Super User
Posts: 6,963

Re: Displaying a letter from the particular address

This is quite easy, then.

%macro extract_letter(varname);
&varname = substr(scan(&varname,-2,'\'),1,1);
%mend;

You just use

%extract_letter(fullpath);

to get the result from your initial example.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: Displaying a letter from the particular address

NOTE: Line generated by the macro variable "VARNAME".
'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx '
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
this error arises when i run the code with a specific address .
Super User
Posts: 6,963

Re: Displaying a letter from the particular address

[ Edited ]

I guess you still have a severe misunderstanding of the macro language. It is a code generator, not code in itself.

My macro is supposed to be used on a variable name, not on a string literal!

That's why the macro parameter is called varname.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎07-26-2016 12:45 AM
Super User
Posts: 6,963

Re: Displaying a letter from the particular address

In case you want to use a macro like a function, you need to define it differently. But I'd rather use a user-defined data step function instead:


%macro extract_letter(instring);
substr(scan(&instring,-2,'\'),1,1) 
%mend;

proc fcmp outlib=work.funcs.test;
function extract_letter(instring $) $;
  return (substr(scan(instring,-2,'\'),1,1));
endsub;
run;

options cmplib=work.funcs;

data want;
fullpath = 'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ';
result1 = extract_letter(fullpath);
result2 = extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
result3 = %extract_letter(fullpath);
result4 = %extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
run;

Note that the results in the dataset from the function will have the desired length of 1, while the results from the macro invocation will default to length 200.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 299 views
  • 0 likes
  • 2 in conversation