BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
boubou31
Calcite | Level 5

Hello guys,
What i want to do here is to soustract a value to my all tables. What i don't understand is that when i'm doing step by step it's working. like wha't follow : 

 

data wilco;
	set test;
	var1=var1-varvec;
	var2=var2-varvec;
	
run;

but i want to do it for 128 variable so you can understand that i don't want to write all the unnecessary line.  I tried something like that :

data wilco;
	set test;
	do i= 1 to 128;
		var&i.= var&i.-varvec;
	end;
	
run;

But in fact it's ignoring my loop part and just do the set part. i'm surely missing something there, and if you could help me to find it ! 
Thanks.

NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      32768 at 85:17   
NOTE: There were 256 observations read from the data set WORK.TEST.
NOTE: The data set WORK.WILCO has 256 observations and 131 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @boubou31 

 

In your example, "i" is not a macrovariable so you can't refer to it as &i.

What you need is an array, to reference a group of columns of the same type.

in the below code, var1 is equivalent to var(1), as this variable is in position 1 in the array. 

Best,

 

data wilco;
	set test;
	array var(128); /* the array 'var' references a group of columns: var1, var2, ... until var 128*/
	do i= 1 to dim(array); /* the array dimension is 128  = the DIM(function) */
		var(i)= var(i)-varvec;
	end;
run;

Best,

View solution in original post

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @boubou31 

 

In your example, "i" is not a macrovariable so you can't refer to it as &i.

What you need is an array, to reference a group of columns of the same type.

in the below code, var1 is equivalent to var(1), as this variable is in position 1 in the array. 

Best,

 

data wilco;
	set test;
	array var(128); /* the array 'var' references a group of columns: var1, var2, ... until var 128*/
	do i= 1 to dim(array); /* the array dimension is 128  = the DIM(function) */
		var(i)= var(i)-varvec;
	end;
run;

Best,

boubou31
Calcite | Level 5

Thanks ! but i'm wondering usually i'm using "i" as a macrovariable when it isn't in a data set and that's working. So it's cause is inside a data set that i cannot use this writing : "&i" ? 

ed_sas_member
Meteorite | Level 14

Hi @boubou31 

You can refer to &i as a macrovariable if you use iterative DO statements inside a macro:

%do i=1 %to 128;

... -> using &i.

%end;

 

Best,

boubou31
Calcite | Level 5
Thanks ! 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 485 views
  • 0 likes
  • 2 in conversation