DATA Step, Macro, Functions and more

Macros and VAR

Reply
Contributor
Posts: 37

Macros and VAR

original code

data p_date;

     process_date = today();

     begin_date = intnx ('day',process_date,-14);

     format [rpcess_date begin_date yymmddp10.;

     call symput("p_date",process_date);

     call sysmput("begin_date", begin_date);

Data test_numbers;

     Infile "case.txt:

     informat ;

     input ;

run;

data data_add;

set test_number;

some code...

run;

proc sql;

   create table detail_&var2 as

    select *

    from data_add_&var2;

quit;

This code works but now I need to change it to a macro with variables;

macro all_records (var1, var2);

data p_date;

     process_date = today();

     begin_date = intnx ('day',process_date,-&var1);

     format [rpcess_date begin_date yymmddp10.;

     call symput("p_date",process_date);

     call sysmput("begin_date", begin_date);

Data test_numbers;

     Infile "&var2..txt:

     informat ;

     input ;

run;

data data_add_&var2;

set test_number;

some code...

run;

proc sql;

   create table detail_&var2 as

    select *

    from data_add_&var2;

quit;

mend;

%all_records (14, Ark);

%all_records (14, Geo);

%all_records (13, Ark);

%all_records (13, Geo);

The only item that correctly converted the var was for the infile.  Error messages are given for the rest.  Can anyone help>

Thanks

PROC Star
Posts: 7,360

Macros and VAR

Without any test data to run your code, I can only make a couple of observations:

1. In both the original and macro you start with a data step creating a file called p_date, but don't appear to use it.  If that is correct, change it to a data _null_ step.  Plus, I would end it with a run statement, although that shouldn't affect anything.

2. You create a file called test_numbers, but then access a file called test_number.  Somehow, I think that is in error.

3. Where you have infile, it looks like a single double quote.  Shouldn't the whole file name be quoted (i.e. have quotes before and after it)?

4. When you call the macro you end the call with a semi-colon.  You probably shouldn't.

HTH,

Art

Contributor
Posts: 37

Macros and VAR

P_date is used in the code that I omited

Test_numbers and Test_numbers is just a typo sorry

The infile works fine with the macro

Not sure about number 4

PROC Star
Posts: 7,360

Macros and VAR

I just noticed that you didn't precede the word macro with a % (at the very start of your macro).  Was that just a typo?

PROC Star
Posts: 7,360

Macros and VAR

I also noticed that you mend statement (at the end) wasn't preceded by a %.  The following appears to work:

%macro all_records (var1, var2);

  data p_date;

     process_date = today();

     begin_date = intnx ('day',process_date,-&var1);

     format process_date begin_date yymmddp10.;

     call symput("p_date",process_date);

     call symput("begin_date", begin_date);

  run;

  Data test_numbers;

     Infile "c:\art\&var2..txt";

     informat x best12.;

     input x;

  run;

  data data_add_&var2;

    set test_numbers;

    *some code...;

  run;

  proc sql;

    create table detail_&var2 as

     select *

      from data_add_&var2;

  quit;

%mend;

%all_records (14, Ark)

Contributor
Posts: 37

Macros and VAR

Yes sorry  the %Macro

The code is not resolving &var1 or &var2 except for the infile

So I was thinking that I am missing either " ' . or something but I have tried putting the &var1 in single and double quotes both will work as long as I take the - sign out of the code.  So I tried %all_records (-14, Ark);  but that did not work either.

for the var2 single or double quotes or period at the end still gave error message about VAR2 not resolved.

Super User
Posts: 17,784

Macros and VAR

Post the error messages you're getting in the log with the section of code that generates the error using options mprint symbolgen to help debug.

It looks like it should work at first glance, so probably some typos or something somewhere.

You can try resolving the macro variables with the period after as well, &var1. though I don't think you need it.

Also can try -1*&var1. but that isn't ideal either...

Ask a Question
Discussion stats
  • 6 replies
  • 233 views
  • 0 likes
  • 3 in conversation