BookmarkSubscribeRSS Feed
Twinklex
Fluorite | Level 6

Hi,

 

I have a requirement where I am trying to place all the column names of a dateset into an array using sas coding. Is there any possibility to that. Thanks in advance!

3 REPLIES 3
maguiremq
SAS Super FREQ

Are all your variables of the same type? If so, you can use the _all_. If not, you have to define them separately by their variable type. You can then use _numeric_ and _character_.

 

See this: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p08do6szetrxe2n136ush727sbuo.htm

 

Specifically, look at the "array elements" section.

FreelanceReinh
Jade | Level 19

Hello @Twinklex,

 

If you want to create an array of character variables, say var1, var2, ..., containing the column names (i.e. variable names) of the original dataset (e.g., SASHELP.CLASS), then try this:

proc sql noprint;
select quote(trim(name)) into: varlist separated by ' '
from dictionary.columns
where libname='SASHELP' & memname='CLASS';
quit;

data want;
array var[&sqlobs] $32 (&varlist);
/* ... code using the array ... */
run;

Or this:

proc transpose data=sashelp.class(obs=0) out=_trans;
var _all_;
run;

proc transpose data=_trans out=_temp(drop=_:) prefix=var;
var _name_;
run;

data want;
set _temp;
array var[*] var:;
/* ... code using the array ... */
run;
ballardw
Super User

Please show an example of your starting data and what you expect the result after this is applied.

It sounds very odd that you want the exact same values for every record in a data set.

 

Note that arrays do not exist after the end of data step code. Array is only a technique for manipulating something while the data step runs. After the data step the only result is variables and their values. So you really do not have much of an array elsewhere.

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