BookmarkSubscribeRSS Feed
littlestone
Fluorite | Level 6
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
7 REPLIES 7
littlestone
Fluorite | Level 6
any suggestion?

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

please...
Peter_C
Rhodochrosite | Level 12
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;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
littlestone
Fluorite | Level 6
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?
Peter_C
Rhodochrosite | Level 12
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');
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Littlestone,

Your code contains an error:

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

Sincerely,
SPR
littlestone
Fluorite | Level 6
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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1185 views
  • 0 likes
  • 3 in conversation