Statistical programming, matrix languages, and more

Create dataset from all vectors whose symbol name starts with letter 'a'

Reply
Occasional Contributor
Posts: 11

Create dataset from all vectors whose symbol name starts with letter 'a'

[ Edited ]

I would like to save all the vectors starting with letter 'a' to a matrix. We can assume that all such vectors are column vectors with the same length. The numbers of vectors whose name start with letter 'a' is variable, in the example below there are 2, but this number is not know at run time. There isn't any pattern in the vector names other than the fact that they start with 'a'

 

 

proc iml;
a_some_name = {1,2,3};
a_some_other_name = {4,5,6};
b = {1,1,1};

create mydata var {a_some_name a_some_other_name ...... a_random_name};
append;
close mydata;

quit;

If I could save the output from 'show names' to a matrix / vector, I could work with that.

Is there a way to do this?

SAS Super FREQ
Posts: 3,839

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Assuming that the names are a1, a2, ..., and the numerical suffixes are consecutive, you can use the index creation operator write the vectors to a data set:

 

k = 2; /* total number of vectors a1, a2, ..., ak */
varNames = "a1":("a"+strip(char(k)));
create mydata var varNames;
append;
close mydata;
Occasional Contributor
Posts: 11

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Rick - my example was poorly formulated and misleading, I apologize for that. There is no pattern in the vector names - I've edited the original post accordingly.
SAS Super FREQ
Posts: 3,839

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

[ Edited ]

Is there any pattern? Did you read them from a data set? If so, what is the naming convention?

Occasional Contributor
Posts: 11

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

There is no pattern that is reproducible (other that all of them starting with 'a' ). The vectors are created within the IML procedure and I could manually add them to the 'create' statement.
However, if they could be added automatically to the data set it would mean less effort for me (and other colleagues who work with similar scripts) - just create the vector definition and it will automatically be included in the export as long as it starts with an 'a'.


Regular Contributor
Posts: 155

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Does this do what you want?

proc iml;
a_some_name = {1,2,3};
a_some_other_name = {4,5,6};
b = {1,1,1};

create mydata var _num_;
append;
close mydata;

quit;

data mydata;
  set mydata(keep=a:);
run;

You might be left with blank rows if there are other larger matrices that are defined in IML.

 

Occasional Contributor
Posts: 11

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Posted in reply to IanWakeling
This won't work because there are a lot of others numeric vectors which I'd rather no write to disk - I didn't know about the _num_ option for 'create' or I would have specified this in the opening post.
Regular Contributor
Posts: 155

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

It might still work, assuming most of the data that you don't want to write to disk is contained is a few matrices.   Then you could delete these matrices before the CREATE using a FREE statement.

Super User
Posts: 10,210

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Interesting thing is you can use keyword _NUM_ to keep all the numeric vectors.
then use another date step to keep all the variables which starts with A.




proc iml;
a1={1,2,3,4};
a2={4,5,6,7};
x={3,6};
create temp var _num_;
append;
close;
quit;

data want;
 set temp;
 keep a: ;
run;

SAS Super FREQ
Posts: 3,839

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

Ian has the right (write?) idea. First write ALL vectors to a data set. Then use a DATA step (inside the SUBMIT statement) to KEEP only those that begin with 'a'. Then read the names back into SAS/IML and write those vectors:

 


proc iml;
a1 = {1,2,3};
a2 = {4,5,6};
b = {1,1,1};
abigal = {0,2,1};
adam = {9,8,7};
k = 2; /* total number of vectors a1, a2, ..., ak */
varNames = "a1":("a"+strip(char(k)));

/* write all numerical vars */
create names var _NUM_; append; close;
/* keep only those that begin with 'a' */
submit;
  data names(keep=a:);
  set names(obs=1);
  run;
endsubmit;

/* read the names of the variables that begin with 'a' */
use names; 
read all var _NUM_ into JUNK[colname=varNames];
close names;
/* write the variables that begin with 'a' */
create mydata var varNames;
append;
close mydata;
Regular Contributor
Posts: 155

Re: Create dataset from all vectors whose symbol name starts with letter 'a'

I think the OP is worried that there is a large amount of other data in IML matrices that will be needlessly written out to a SAS data set.   So a simple modification to Rick's code will avoid this.   The 1st append statement can be removed, so only the headers are written to the names data set.   Then the code in the submit block needs to be:

  data names;
    if 1=2 then set names(keep=a:);
    output;
  run;

This creates a singe record of missing values as there needs to be something to read back in to the JUNK matrix.

 

Ask a Question
Discussion stats
  • 10 replies
  • 368 views
  • 5 likes
  • 4 in conversation