BookmarkSubscribeRSS Feed
mick_g
Calcite | Level 5

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

6 REPLIES 6
art297
Opal | Level 21

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

mick_g
Calcite | Level 5

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

art297
Opal | Level 21

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?

art297
Opal | Level 21

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)

mick_g
Calcite | Level 5

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.

Reeza
Super User

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...

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 992 views
  • 0 likes
  • 3 in conversation