SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
feyzi
Calcite | Level 5

Hi there,

I'm not sure how easy it is to do this but does anyone know how I can create new variables in a Do Loop, where the values being looped through will be part of the variable names.

So say I want to create 10 variables named a1 - a10. I was thinking I could do this in a do loop as below, but not sure how.

Note: I'm using 10 here as an example. In reality I'll swap the 10 with a macro variable.

DATA DESIGN;

  SET DESIGN;

  DO J = 1 TO 10;

     /* Here I want to create the variables a1 - a10 */

  END;

RUN;

Many thanks

Emre

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

May be arrays will help you

DATA DESIGN;

  SET DESIGN;

array new(10) a1-a10;

  DO J = 1 TO 10;

      new(j)=values;

     /* Here I want to create the variables a1 - a10 */

  END;

RUN;


Thanks,

Jag

Thanks,
Jag

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

May be arrays will help you

DATA DESIGN;

  SET DESIGN;

array new(10) a1-a10;

  DO J = 1 TO 10;

      new(j)=values;

     /* Here I want to create the variables a1 - a10 */

  END;

RUN;


Thanks,

Jag

Thanks,
Jag
Tom
Super User Tom
Super User

You can "create" new variables by just referencing them.

%let nvars=10 ;

You could just give them a length;

  length a1 - a&nvars 8 ;

Or you could define an array ;

  array a(&nvars) ;

If you want to know how to assign them specific values then you will need to provide more information on what you want.

feyzi
Calcite | Level 5

Thanks guys!

Much appreciated!

Emre

Code in the end:

DATA DESIGN;

  SET DESIGN;

  RETAIN I(0);

  I = I + 1;

  ARRAY A(&XVAR) a1 - a&XVAR;

  DO J = 1 TO &XVAR;

       IF I = J THEN A(J) = 1;

  END;

  ARRAY NUMS _NUMERIC_;

       DO OVER NUMS;

       IF NUMS= '.' THEN NUMS = 0;

  END;

RUN;

Tom
Super User Tom
Super User

Here is a way to create an identity matrix.

%let xvar=10 ;

%let base=A;

data identity&xvar ;

  array &base(&xvar) (&xvar*0) ;

  do _n_=1 to &xvar ;

    &base(_n_) = 1;

    output;

    &base(_n_) = 0;

  end;

run;

proc print; run;

feyzi
Calcite | Level 5

Hi Tom,

Thanks for the tip.

I'm using PROC FCMP for the identity.

Now I need to create a diagonal from an array in PROC FCMP.

Let's see how that goes.

Thanks

Emre

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 18176 views
  • 4 likes
  • 3 in conversation