DATA Step, Macro, Functions and more

insert data set into 2-dimensional array

Reply
Contributor
Posts: 60

insert data set into 2-dimensional array

Is there a possibility to easily insert data set into 2-dimensional  array?

The data set can have variable column count and names.

Is there a way to reference columns by position instead by column name in data step?

Respected Advisor
Posts: 3,777

Re: insert data set into 2-dimensional array

I would consider loading the data set into a hash object.  Perhaps with an iterator so you can treat it like and array using the iterator methods FIRST NEXT and the rest.

Super User
Posts: 9,682

Re: insert data set into 2-dimensional array

"Is there a possibility to easily insert data set into 2-dimensional  array?"

Yes. EASY. but you only can hold one type variables, that means numeric or character .

data class;

set sashelp.class;

keep _numeric_;

run;

%let dsid=%sysfunc(open(class,i));

%let nobs=%sysfunc(attrn(&dsid,nobs));

%let nvar=%sysfunc(attrn(&dsid,nvar));

%let dsid=%sysfunc(close(&dsid));

data _null_;

set class end=last;

array a{&nvar] _numeric_;

array _a{&nobs,&nvar} _temporary_;

do j=1 to dim(a);

    _a{_n_,j}=a{j};

  end;

if last then do;

do m=1 to &nobs;

  do n=1 to &nvar;

   put _a{m,n}= ;

  end;

end;

end;

run;

"The data set can have variable column count and names.

Is there a way to reference columns by position instead by column name in data step?"

Yes. There is a function to get it. Check it at SAS Documentation.

Ksharp

Ask a Question
Discussion stats
  • 2 replies
  • 153 views
  • 0 likes
  • 3 in conversation