data main;
name='sas communities' ;
run;
how to keep in macro variable every letter of name variable?
You can set the value of name in a macro variable by:
%lat name = sas communities;
or create it in your step by
data main;
name='sas communities' ;
call symput ('NAME', name);
run;
Anyhow, what to you mean by "every letter of name variable" ?
Please post your target.
@thanikondharish wrote:
Every letter in that string
Run @Shmuel's code, and you'll see that it works. Show at least a little initiative on your part, please.
To assign every letter of the string in a separate macro variable you need a loop:
either:
%macro mac_name(string);
%do n=1 %to %length(&string);
%let L&n = %substr(&string, &n,1);
%end;
%mend mac_name;
%mac_name(sas comunities);
or
data _null_;
name = 'sas comunities';
do n=1 to length(name);
call symput('L'||n , substr(name,n,1);
end;
run;
to check results run next code:
%put &L1 &L2 &L3 ... &LX;
data main;
name="sas communities";
new=compress(name);
do i = 1 to length(new);
var=substr(new,i,1);
output;
end;
run;
proc sql;
select var into: var1 - from main;
quit;
%put &var1 &var2 &var3 &var4;
You will need to define a macro, since %DO is not allowed outside of a macro. So within a macro:
%let name = sas communities;
%local k next_letter;
%global letter_list;
%let letter_list=;
%do k = 1 %to %length (&name);
%let next_letter = %substr(&name, &k, 1);
%if %index(&name, &next_letter) = 0 %then %let name = &name&next_letter;
%end;
The final value of &LETTER_LIST will be a list of all the letters found in &NAME, with duplicates eliminated.
Why is it that you are trying to use macros? It was only a few weeks ago that you were struggling with basic SAS language questions. It is probably too early to worry about macros. Your time would be better spent learning more SAS skills first. If it appeals to you, I could give you a list of topics to learn.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.