DATA Step, Macro, Functions and more

Do Looping into macro

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Do Looping into macro

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


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 6,500

Re: Do Looping into macro

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;

image.png

View solution in original post


All Replies
Super User
Posts: 17,831

Re: Do Looping into macro

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. 

New Contributor
Posts: 3

Re: Do Looping into macro

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)

 

Solution
2 weeks ago
Super User
Super User
Posts: 6,500

Re: Do Looping into macro

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;

image.png

New Contributor
Posts: 3

Re: Do Looping into macro

Thanks Tom. 

 

This will solve my pupose. Just for my curiosity , can this be converted in macro?

Super User
Super User
Posts: 6,500

Re: Do Looping into 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.

Super User
Posts: 10,500

Re: Do Looping into macro


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.

 

Trusted Advisor
Posts: 1,378

Re: Do Looping into macro

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;

Super User
Posts: 6,938

Re: Do Looping into macro

[ Edited ]

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;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 188 views
  • 4 likes
  • 6 in conversation