I want to create my dataset using macro which should have 10 variables (e.g. Var1 to Var10) and want to insert values using iteration. something like
Var1 Var2 Var3 Var4 Var5...........Var10
1 2 3 4 5
I want 1 2 3 4...10 using iteration in dataline statement.
Code I tried is(which is not working):
%macro sqr_cub(iteration);
data iteration(drop = j);
infile datalines;
input var_1-Var_10;
datalines;
do j = 1 to &iteration.;
x = j**2;
output ;
end;
run;
%Mend;
%sqr_cub(10);
Please help
So perhaps something like this?
data want ;
do row=1 to 3 ;
array var (10) ;
do i=1 to dim(var);
var(i)=i**row;
end;
output;
end;
drop i ;
run;
Datalines/cards doesn't work in a macro for starters.
Try referencing a text file or a SAS dataset instead. If you really need datalines inside the macro loop at the SAS 9.4 Macro appendix in the documentation. It has an example of how to work around this issue.
Thanks for quick revert.
As I am new to it and have no idea of exact procedure, But my query is to create a dataset like below using iteration.
Var1 Var2.....Var10
1 2.........10
1 4..........100(square)
1 8..........1000(cube)
So perhaps something like this?
data want ;
do row=1 to 3 ;
array var (10) ;
do i=1 to dim(var);
var(i)=i**row;
end;
output;
end;
drop i ;
run;
Thanks Tom.
This will solve my pupose. Just for my curiosity , can this be converted in macro?
You can pretty much convert anything to a macro (EXCEPT using in-line DATALINES/CARDS).
What part of that data step would you want the macro to generate?
If you just want to make the number of observations generated variable then replace the 3 with a macro variable reference and set the value into the macro variable.
@SRakesh wrote:
Thanks Tom.
This will solve my pupose. Just for my curiosity , can this be converted in macro?
Like this?
%macro MakePowers(set=want, maxrow=3, maxpow=10); data &set ; do row=1 to &maxrow ; array v (&maxpow) ; do i=1 to dim(v); v(i)=i**row; end; output; end; drop i ; run; %mend; %makepowers; /*uses defaults*/ %makepowers(set=data_5_15,maxrow=5,maxpow=15);
Minor difference of array v and variables v1 to vnn. VAR is a SAS function in the data step and I don't like using variables with function names generally.
Your issue is not the result of using macro program but of wrong order of statment in your code.
1) You can simplify your code by assigning value to your macro variable using %LET statemant
instead of using a %macro program.
2) The right order of using instream input by CARDS or DATALINES is:
data have;
INFILE DATALINES;
INPUT var1 var2 ...;
... any code dealing with the variables like computations, selections etc. ...
DATALINES;
... instream data to read ...
; /* semicolon to close instraem data */
RUN;
Your SAS code as such is invalid. Just try it on its own, without a macro wrapped around it:
%let iteration=5;
data iteration(drop = j);
infile datalines;
input var_1-Var_10;
datalines;
do j = 1 to &iteration.;
x = j**2;
output ;
end;
run;
Log:
24 %let iteration=5; 25 26 data iteration(drop = j); 27 infile datalines; 28 input var_1-Var_10; 29 datalines; WARNING: Die Variable j in der DROP-, KEEP- oder RENAME-Liste wurde nie referenziert. NOTE: The data set WORK.ITERATION has 0 observations and 10 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 30 do j = 1 to &iteration.; 30 do j = 1 to &iteration.; __ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 31 x = j**2; _ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 32 output ; ______ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 33 end; 2 Das SAS System 08:04 Wednesday, August 9, 2017 ___ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 34 run;
The first line after datalines; breaks the datalines section and causes the end of the data step compilation, resulting in the messages.
Strong hint for macro programming: first create base SAS code that works, then replace parts with macro variables and test again, and when this works, wrap into a macro with the macrovars as parameters.
In your case, instead of abusing the macro processor, write a simple data step as @Tom suggested:
data want ;
do row=1 to 3 ;
array var (10) ;
do i=1 to dim(var);
var(i)=i**row;
end;
output;
end;
drop i ;
run;
Now replace values with macrovars:
%let rows=3;
%let iterations=10;
data want ;
do row=1 to &rows ;
array var (&iterations) ;
do i=1 to dim(var);
var(i)=i**row;
end;
output;
end;
drop i ;
run;
Then, wrap into a macro:
%macro mymac(rows,iterations);
data want ;
do row=1 to &rows ;
array var (&iterations) ;
do i=1 to dim(var);
var(i)=i**row;
end;
output;
end;
drop i ;
run;
%mend;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.