DATA Step, Macro, Functions and more

how to keep in macro variable

Reply
Frequent Contributor
Posts: 87

how to keep in macro variable

data main;

name='sas communities' ;

run;

 

 

 

how to keep   in macro variable every letter of name variable?

Trusted Advisor
Posts: 1,848

Re: how to keep in macro variable

[ Edited ]
Posted in reply to thanikondharish

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.

 

Frequent Contributor
Posts: 87

Re: how to keep in macro variable

Every letter in that string
Super User
Posts: 10,574

Re: how to keep in macro variable

Posted in reply to thanikondharish

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Trusted Advisor
Posts: 1,848

Re: how to keep in macro variable

Posted in reply to thanikondharish

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;
Trusted Advisor
Posts: 1,163

Re: how to keep in macro variable

Posted in reply to thanikondharish
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;
Thanks,
Jag
Frequent Contributor
Posts: 87

Re: how to keep in macro variable

Posted in reply to Jagadishkatam
Use %do %substr
Super User
Posts: 6,934

Re: how to keep in macro variable

Posted in reply to thanikondharish

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.

Ask a Question
Discussion stats
  • 7 replies
  • 84 views
  • 1 like
  • 5 in conversation