BookmarkSubscribeRSS Feed
Vasundha
Calcite | Level 5

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. 
6 REPLIES 6
Patrick
Opal | Level 21

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.

Patrick_1-1676956455392.png

 

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;

Patrick_2-1676956825190.png

 

 

Vasundha
Calcite | Level 5
Thanks for responding. It's necessary for me to use macro language in my code to reduce the complexity as i've more than 5 columns are of same type of values in my table and i do have terms less than 12.
Kurt_Bremser
Super User

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
Calcite | Level 5
I get it what I require instead of 12 I should use 1. That's my bad. Anyways, thankyou.
Patrick
Opal | Level 21

@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.

ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 945 views
  • 0 likes
  • 4 in conversation