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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 306 views
  • 4 likes
  • 2 in conversation