BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data main;

name='sas communities' ;

run;

 

 

 

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

7 REPLIES 7
Shmuel
Garnet | Level 18

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
Calcite | Level 5
Every letter in that string
Shmuel
Garnet | Level 18

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;
Jagadishkatam
Amethyst | Level 16
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
thanikondharish
Calcite | Level 5
Use %do %substr
Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1354 views
  • 1 like
  • 5 in conversation