BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fafrin420
Fluorite | Level 6
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
novinosrin
Tourmaline | Level 20
/*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;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

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.

fafrin420
Fluorite | Level 6
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.

novinosrin
Tourmaline | Level 20
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
fafrin420
Fluorite | Level 6

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

 

novinosrin
Tourmaline | Level 20

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. 

fafrin420
Fluorite | Level 6

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.

Dataset input.JPGDataset output.JPG

 

 

novinosrin
Tourmaline | Level 20

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

fafrin420
Fluorite | Level 6
SomethingSum
112
213
114
313
112
513
114
213

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

novinosrin
Tourmaline | Level 20
/*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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1905 views
  • 0 likes
  • 3 in conversation