BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
javvadi
Calcite | Level 5

Dear all,

%let var1=a;

%let var2=b;

%let var3=c;

%let var4=d;

%let var5=e;

%let x= &var1 %to &var5;

i need x=a,b,c,d,e

thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi,

If you really want to make use of "%to" for larger lists then you'll need to use it inside a macro definition, e.g.:

%let var1=a;

%let var2=b;

%let var3=c;

%let var4=d;

%let var5=e;

%macro makecsv(root,fromval,toval);

  %let sep=;

  %let makecsv=;

  %do csvloop=&fromval %to &toval;

    %let makecsv=&makecsv&sep&&&root&csvloop;

    %let sep=%str(,);

  %end;

  &makecsv;

%mend makecsv;

%let x=%makecsv(var,1,5);

%put x=&x;

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19
17         %let var1=a;
18         %let var2=b;
19         %let var3=c;
20         %let var4=d;
21         %let var5=e;
22        
23        
24         %let x = &var1,&var2,&var3,&var4,&var5;
25        
26         %put NOTE: &=x;
NOTE: X=a,b,c,d,e
Amir
PROC Star

Hi,

If you really want to make use of "%to" for larger lists then you'll need to use it inside a macro definition, e.g.:

%let var1=a;

%let var2=b;

%let var3=c;

%let var4=d;

%let var5=e;

%macro makecsv(root,fromval,toval);

  %let sep=;

  %let makecsv=;

  %do csvloop=&fromval %to &toval;

    %let makecsv=&makecsv&sep&&&root&csvloop;

    %let sep=%str(,);

  %end;

  &makecsv;

%mend makecsv;

%let x=%makecsv(var,1,5);

%put x=&x;

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

Haikuo
Onyx | Level 15

So there could be many solutions, but do remember SAS Macro is nothing but a text string maneuver.

%let var1=a;

%let var2=b;

%let var3=c;

%let var4=d;

%let var5=e;

%macro cat;

%global x;

%let x=;

  %do i=1 %to 5;

    %if &i=1 %then %let x=&var1;

      %else  %let x=&x,&&var&i;

  %end;

%mend;

%cat;

%put &x;

    

Haikuo

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
  • 3 replies
  • 960 views
  • 4 likes
  • 4 in conversation