Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
KfirDarina
Level II

Create local variables that hold lists from column names

Hi all, 

Need your help with creating multiple local variables that contain column names within var name.

I'v tried this butget an err.

Any suggestions?


for(j=1,j<=ncol(dt),j++,
local(parse(char(column name(j))||"key")=Associative Array(column(j)<<Get Values)<<get keys));

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
ian_jmp
Staff

Re: Create local variables that hold lists from column names

OK. Here's one way, and I'm sure there will be others:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

varList = {};
For( j = 1, j <= N Col( dt ), j++,
	varName = (Column(dt, j ) << getName)||"_keys";
	keys = Associative Array( Column(dt, j ) << Get Values ) << get keys;
	makeVar = Expr(LHS = RHS);
	SubstituteInto(makeVar, Expr(LHS), Parse(varName), Expr(RHS), Eval(keys));
	makeVar;
	InsertInto(varList, Parse(varName));
);
Print(varList);
Print(EvalList(varList));

View solution in original post

4 REPLIES 4
Highlighted
ian_jmp
Staff

Re: Create local variables that hold lists from column names

Not sure I understand your use of 'Local()', but is this close to what you want?

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

keyVarList = {};
For( j = 1, j <= N Col( dt ), j++,
	keys = Associative Array( Column( j ) << Get Values ) << get keys;
	InsertInto(keyVarList, EvalList(List(keys)));
);

for(j = 1, j <= NItems(keyVarList), j++,
	Print(keyVarList[j]);
);
Highlighted
KfirDarina
Level II

Re: Create local variables that hold lists from column names

ian thx,

My intention is that for each list that was created by associative aray function it will also have a unique variable name that contain the matched column name. 

For example: if I have two columns: Entity and Lot, then I will get two local variables :

 

Entitykey={entity1, entity2}

Lotkey={Lot1,Lot2}....

And so on for all columns in table

Highlighted
ian_jmp
Staff

Re: Create local variables that hold lists from column names

OK. Here's one way, and I'm sure there will be others:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

varList = {};
For( j = 1, j <= N Col( dt ), j++,
	varName = (Column(dt, j ) << getName)||"_keys";
	keys = Associative Array( Column(dt, j ) << Get Values ) << get keys;
	makeVar = Expr(LHS = RHS);
	SubstituteInto(makeVar, Expr(LHS), Parse(varName), Expr(RHS), Eval(keys));
	makeVar;
	InsertInto(varList, Parse(varName));
);
Print(varList);
Print(EvalList(varList));

View solution in original post

Highlighted
KfirDarina
Level II

Re: Create local variables that hold lists from column names

ian thx. this is great, i was battling for 2 days to solve it, appreciate that :)
Article Labels

    There are no labels assigned to this post.