Hello Experts,
I'd be grateful if you could assist me on this.
I've a table with some columns named Code1-Code5. In these columns I've values separated with colon (:). So, for that I've written a macro .
Here's it is.
%macro abc; Data xyz; Set xyz; %do i =1 %to 5; Code_C&i. = scan(Code&I., 12, ' : ') ; %end; run; %mend abc; %abc In this code, the new variables are getting created but there are no values in it. Im having blank values in them. I've no clue why it isn't providing values the way it is supposed to. Please help me out on this.
Your code works and does what you're asking it to do but.... don't use macro language if not necessary.
In your code you're extracting the 12th term. If there are less than 12 terms then the result will be missing.
Converting your code to data step syntax only:
data xyz;
length code1 - code5 $30;
code1='aa:bb';
code4='a:a:a:a:a:a:a:a:a:a:B:C:D:E';
run;
data xyz;
set xyz;
array _code {*} code1 - code5;
array code_C {5} $30;
do _i=1 to dim(_code);
code_C[_i] = scan(_code[_i], 12, ' : ');
end;
drop _i;
run;
proc print data=xyz;
run;
You DO NOT NEED a macro. Expanding an array is easier than expanding macro code.
The list of variables can be determined in a preceding step and stored in a macro variable, but you positively do not need a macro.
Don't be obsessed with (ab)using the macro language just because it's there.
@Vasundha wrote:
I get it what I require instead of 12 I should use 1. That's my bad. Anyways, thankyou.
If it's the first term you're after then yes, you need 1 ...and that's why I've created such sample data to show you what happens.
And about macro language: I've shared with you fully working data step code that demonstrates that you don't need macro language.
By the way your SCAN is including SPACE as a delimiter. So If one of your values in delimited list includes a space that is counted as well. If you do not want to use a space as a delimiter do not include them in the third parameter
Instead of
Code_C&i. = scan(Code&I., 12, ' : ') ;
to not include space as delimiter
Code_C&i. = scan(Code&I., 12, ':') ;
Data as an example is helpful because otherwise we have to make guesses as to what valid values may look like and quite often those guesses can be wrong.
@Vasundha wrote:
Hello Experts,
I'd be grateful if you could assist me on this.
I've a table with some columns named Code1-Code5. In these columns I've values separated with colon (:). So, for that I've written a macro .
Here's it is.
%macro abc; Data xyz; Set xyz; %do i =1 %to 5; Code_C&i. = scan(Code&I., 12, ' : ') ; %end; run; %mend abc; %abc In this code, the new variables are getting created but there are no values in it. Im having blank values in them. I've no clue why it isn't providing values the way it is supposed to. Please help me out on this.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.