Fluorite | Level 6

## Calling numeric variable from a list in a loop

``````data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
;

proc sql noprint;
select x into :List separated by ' '
from A;
quit;
%put List = &List;

%let N2 = %sysfunc(countw(&List));

%macro ratio;
data DataSET; set DataSET;
%do i=1 %to &N2;
%let var = %scan(&List, &i);
Total = Value + &List;

%end;
run;``````

Hi,

I want to store numeric variables in a list. And then call each variable from that list (in a loop) and use that variable to do some operations. The above program is not being able to read the variables from the list as numeric. Can you suggest a better way to do it?

My error log says "Expecting an arithmatic operator"

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

``````/*input dataset 1*/
data have;
input something sum;
cards;
1 12
2 13
1 14
3 13
1 12
5 13
1 14
2 13
;
/*input dataset 2*/
data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6
;

proc sql;
select count(*) into :n
from a;
quit;

proc transpose data=a out=_a prefix=a;
var x;
run;

data want;
set have;
array total(&n);
if _n_=1 then set _a;
array t(*) a:;
do _n_=1 to dim(t);
if something=1 then total(_n_)=t(_n_)+sum;
else total(_n_)=sum;
end;
keep something sum total:;
run;``````
9 REPLIES 9
Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

There are several things wrong in your code and it is not clear what you are trying to accomplish.

First of all, you are not storing any variables in any list. In your SQL Procedure, you simply save the ten numbers defined in the previous data step.

Next, you do this

``%let N2 = %sysfunc(countw(&List));``

and I assume you want N2 to equal 10 since you have ten numbers in the macro variable List. However this code

``%put N2=&N2;``

reveals that N2=19.

Finally, you try to define some macro, but you have no %MEND Statement.

Be more specific about what you are trying to accomplish.

Fluorite | Level 6

## Re: Calling numeric variable from a list in a loop

``````data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
;

proc sql noprint;
select x into :List separated by ' '
from A;
quit;
%put List = &List;

%let N2 = %sysfunc(countw(&List));

%macro ratio;
data DataSET; set DataSET;
%do i=1 %to &N2;
%let var = %scan(&List, &i);
Total = 10 + &List;

%end;
run;
%mend;

%ratio;``````

Hi, Sorry I missed the %mend earlier. My goal is to make a loop that would add each variable from the list to the 10 to get me total.

So Total will be 10.1, 10.2, 10.3 etc.

Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

``````data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
;

proc sql noprint;
select x into :List separated by ','
from A;
quit;
%put List = &List;

%let N2 = %sysfunc(countw(%bquote(&List),%str(,)));
%put &n2;

%macro ratio;

%do i=1 %to &N2;
%let var = %scan(%bquote(&List), &i,%str(,));
%put  var=&var;
%let Total = %sysevalf(10 + &var);
%put total =&total ;
%end;
%mend;
%ratio``````
Fluorite | Level 6

## Re: Calling numeric variable from a list in a loop

Hi Novinosrin,

Thanks a lot for the help. I have one more question. Suppose I want to add the value from the list to another numeric variable "sum" from the dataset B with an "if" Statement. Suppose,

If something=1 then Total = Sum + Value from the list. Can you please let me know a way to do so?

``````data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
;

proc sql noprint;
select x into :List separated by ','
from A;
quit;
%put List = &List;

%let N2 = %sysfunc(countw(%bquote(&List),%str(,)));
%put &n2;

%macro ratio;
Data B; set B;
%do i=1 %to &N2;
%let var = %scan(%bquote(&List), &i,%str(,));
%put  var=&var;
%let if Something =1 then Total = %sysevalf(10 + &var);
%put total =&total ;
%end;
%mend;
%ratio``````

Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

Good morning @fafrin420  Your requirement sounds like can be comfortably and easily accomplished without the need of macro to generate a sas code.  Please provide a comprehensive and nice representative  sample of your datasets and your output dataset(your expected) with a line saying what you want to accomplish.

Once you give us that full info, and if you still insist on macros, of course we shall compare and do both.

Fluorite | Level 6

## Re: Calling numeric variable from a list in a loop

Good morning @novinosrin. Here are my input and output datasets. My goal is to find total1, total2, total3, total4, total5, total6 by adding sum to each value .1,.2,.3,.4,.5,.6 given that something=1.

Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

Hi @fafrin420 I am unable to copy paste the pic data, can you paste that as plain text plz?

Fluorite | Level 6

## Re: Calling numeric variable from a list in a loop

 Something Sum 1 12 2 13 1 14 3 13 1 12 5 13 1 14 2 13

Hi, Here is the input data set.

For the values to be added to sum you have:

0.1, 0.2, 0.3, 0.4, 0.5, 0.6

Tourmaline | Level 20

## Re: Calling numeric variable from a list in a loop

``````/*input dataset 1*/
data have;
input something sum;
cards;
1 12
2 13
1 14
3 13
1 12
5 13
1 14
2 13
;
/*input dataset 2*/
data A;
input x @@;
datalines;
0.1 0.2 0.3 0.4 0.5 0.6
;

proc sql;
select count(*) into :n
from a;
quit;

proc transpose data=a out=_a prefix=a;
var x;
run;

data want;
set have;
array total(&n);
if _n_=1 then set _a;
array t(*) a:;
do _n_=1 to dim(t);
if something=1 then total(_n_)=t(_n_)+sum;
else total(_n_)=sum;
end;
keep something sum total:;
run;``````
Discussion stats
• 9 replies
• 1874 views
• 0 likes
• 3 in conversation