BookmarkSubscribeRSS Feed
rdum96
Calcite | Level 5

Hi,

I have a dataset with existing variables category_1-category_n. I want to create another array 'price' being price_1-price_n. Depending on the which version of the data I'm using the value of 'n' can change.

I tried this:

array cats $ category: ;

array price {dim(cats)} price:;

 

which did not work. Would there be a way to achieve this?

 

 

1 REPLY 1
Tom
Super User Tom
Super User

To define new variables you need to know how many to create BEFORE that data step even starts running so there is no way the syntax you proposed could work (even if SAS modified the language to allow it).

 

You might try this method to create a macro variable first and then use that in the data step.

data _null_;
  if 0 set have ;
  array cats category: ;
  call symputx('nvars',dim(cats));
  stop;
run;
data want;
  set have ;
  array cats category: ;
  array price [&nvars] ;
  ...

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 385 views
  • 4 likes
  • 2 in conversation