Macro with data steps

Reply
Occasional Contributor Pan
Occasional Contributor
Posts: 10

Macro with data steps

I cannot figure out the error of the following code, please help, thanks. The log says "set is not valid". Data set of "Det_2012" is existing.

%Macro Test (YR);

%let P_YR=%sysevalf(%eval(&YR - 1));

Data Det_&YR;

Set Det_&P_YR;

Run;

%mend Test;

%Test(2013)

Super User
Posts: 19,837

Re: Macro with data steps

Post the full log.

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

data Det_2012;

data input Name &  age birth_yr;

datalines;

Josh 36 1977

Mary 24 1983

;

run;

%Macro Test (YR);

%let P_YR=%sysevalf(%eval(&YR - 1));

Data Det_&YR;

Set Det_&P_YR;

Year=&YR;

if name="Josh" then age= Year-birth_yr;

Run;

%mend Test;

%Test(2013)

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

note: Line generated by the invoked macro "Test".

set

-----

statement is not valid or it is used ourt of proper order.

Super User
Posts: 19,837

Re: Macro with data steps

I believe this is what you meant:

data Det_2012;

input Name $  age birth_yr;

datalines;

Josh 36 1977

Mary 24 1983

;

run;

Your det_2012 create step is wrong, but the macro runs for me.

403  data Det_2012;

404

405  input Name $  age birth_yr;

406

407  datalines;

NOTE: SAS went to a new line when INPUT statement reached past the end of a line.

NOTE: The data set WORK.DET_2012 has 2 observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.06 seconds

      cpu time            0.00 seconds

413  ;

414

415  run;

416  %Macro Test (YR);

417

418  %let P_YR=%sysevalf(%eval(&YR - 1));

419

420

421

422  Data Det_&YR;

423

424  Set Det_&P_YR;

425

426  Year=&YR;

427

428  if name="Josh" then age= Year-birth_yr;

429

430  Run;

431

432  %mend Test;

433

434  %Test(2013)

NOTE: There were 2 observations read from the data set WORK.DET_2012.

NOTE: The data set WORK.DET_2013 has 2 observations and 4 variables.

NOTE: DATA statement used (Total process time):

      real time           0.04 seconds

      cpu time            0.01 seconds

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

Thanks for testing this for me. That is weird, it should work like yours but not on my side. maybe it is the version thing?

Super User
Posts: 19,837

Re: Macro with data steps

Post your full log, not just the error section. Usually an error like that means you forget a semicolon in your data step statement, or that line didn't work.

The log will highlight that issue.

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

Thanks guys for your reply. I closed the code and reopened it and rerun it . Then it works. I had no idea why is that, maybe because my macro did not stop? But, hey I can continue my work now. Thanks you all!

Trusted Advisor
Posts: 1,931

Re: Macro with data steps

There's also no reason to use %sysevalf here

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

Posted in reply to PaigeMiller

Yes, you are right. no need for sysevalf here. Thanks

Trusted Advisor
Posts: 1,931

Re: Macro with data steps

I believe that the request was for you to post the FULL SASLOG

Nevertheless, the 2nd line is the problem, it does not allow Det_2012 to be created because the 2nd line is incorrect.

It should read

input Name $ age birth_yr;

Occasional Contributor Pan
Occasional Contributor
Posts: 10

Re: Macro with data steps

Posted in reply to PaigeMiller

Thanks for your reply. it is my typo here. I actually don't have that error at my sas code. the data " Det_2012" is existing.

see my second reply, that is the log. looks like it did not read " set" at data steps as it should.

Ask a Question
Discussion stats
  • 11 replies
  • 258 views
  • 0 likes
  • 3 in conversation