Help using Base SAS procedures

how to use variables from another data step?

Reply
Frequent Contributor
Posts: 89

how to use variables from another data step?

dear all,
suppose I have following codes:

data test; /*data step 1*/
input var1-var6;
cards;
1 1 1 1 1 1
;
run;

data _null_; /*data step 2*/
dsid = open ('work.test');
NumOfVar = attrn (dsid,'NVAR');
run;

What I want to do is: I want to use the variable NumOfVar created by data step 2 , and use it in data step 3, as following:

data test2; /*data step 3*/
array NewVar(NumOfVar);
do i=1 to NumOfVar;
newvar(i)=i;
end;
run;

Is there a way to make it possible? thanks. Message was edited by: littlestone
Frequent Contributor
Posts: 89

Re: how to use variables from another data step?

any suggestion?

or did I violate any SAS coding rules in my codes?

please...
Valued Guide
Posts: 2,174

Re: how to use variables from another data step?

pass the value as a macro variable

data _null_ /*data step 2*/
dsid = open ('work.test');
NumOfVar = attrn (dsid,'NVAR');
call symputx( 'numOfvar', numofVar ); * push the value into macro var ;
run;

option symbolgen ;
data test2; /*data step 3*/
array NewVar(&NumOfVar) ; * referring to the value ;
do i=1 to &NumOfVar;
newvar(i)=i;
end;
run;
Super Contributor
Super Contributor
Posts: 365

Re: how to use variables from another data step?

Hello Littlestone,

I made some changes in your code:
[pre]
data _null_; /*data step 2*/
dsid = open ('work.testl');
NumOfVar = attrn (dsid,'NVAR');
call SYMPUTX('NumOfVar',NumOfVar);
run;
data test2; /*data step 3*/
array NewVar{&NumOfVar} newvar1-newvar&NumOfVar;
do i=1 to &NumOfVar;
newvar[i]=i;
end;
run;
[/pre]
Sincerely,
SPR

Message was edited by: SPR Message was edited by: SPR
Frequent Contributor
Posts: 89

Re: how to use variables from another data step?

I am so sorry for the late response ( my boss wants me to do sth else ...).

I tested both codes; unfortunately, neithe go through.

Here is one code I tested:

data test; /*data step 1*/
input var1-var6;
cards;
1 1 1 1 1 1
;
run;

data _null_; /*data step 2*/
dsid = open ('work.testl');
NumOfVar = attrn (dsid,'NVAR');
call SYMPUTX('NumOfVar',NumOfVar);
run;

data test2; /*data step 3*/
array NewVar{&NumOfVar} newvar1-newvar&NumOfVar;
do i=1 to &NumOfVar;
newvar=i;
end;
run;

Can someone tell me what is wrong with the code?
Valued Guide
Posts: 2,174

Re: how to use variables from another data step?

you create work.test but then try to open work.test1
Possibly you get a message like[pre]623 dsid = open ('work.testl');
624 NumOfVar = attrn (dsid,'NVAR');

NOTE: Argument 1 to function ATTRN at line 624 column 14 is invalid.
dsid=0 NumOfVar=. _ERROR_=1 _N_=1[/pre]Notice the value of DSID=0
If you had been able to open TEST1 then DSID would have had a value greater than zero.
try again with [pre] dsid = open ('work.test');
Super Contributor
Super Contributor
Posts: 365

Re: how to use variables from another data step?

Hello Littlestone,

Your code contains an error:

instead of dsid = open ('work.TESTL');
should be dsid = open ('work.TEST');

Sincerely,
SPR
Frequent Contributor
Posts: 89

Re: how to use variables from another data step?

Thank you so much for helping me.
I did not realize I made such an obvious mistake. I truly apologize for that.

I ran the code again and it works!
Here is the working codes:

data test; /*data step 1*/
input var1-var6;
cards;
1 1 1 1 1 1
;
run;

data _null_; /*data step 2*/
dsid = open ('work.test');
NumOfVar = attrn (dsid,'NVAR');
call SYMPUTX('NumOfVar',NumOfVar);
run;

data test2; /*data step 3*/
array NewVar(&NumOfVar) newvar1-newvar&NumOfVar;
do i=1 to &NumOfVar;
newvar(i)=i;
end;
run;

Thank you all very much for helping me out!
Ask a Question
Discussion stats
  • 7 replies
  • 121 views
  • 0 likes
  • 3 in conversation