DATA Step, Macro, Functions and more

Lost with macro calls

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Lost with macro calls

I have several datasets, one for each geographical areas. What is a bit hard is that there are 96 areas, but for historical and administrative reasons, they are not indexed 01 to 96 but

01 02 ... 19 2A 2B 21 ...95

Then my datasets are indexed data_d01, data_d02, ... data_d19, data_d2a, data_d2b, data_d21, ... data_d95.

So what has been done until now and what I have trying to use is a table that has one variable, called dep2, which takes these values 01 02 ... 19 2A 2B 21 ...95

Then the  way to do something for each dataset is to use the following lines

%macro MacroStandard();

    data _null_;

    set dep.dep2; /* table des (vrais) DEP reprise de RFL, POTE n'est plus par dir fiscale */

    call symput('d'!!left(_n_),dep2); /* la var contenant le code du DEP s'appelle dep2 */

    run;

    %do i=1 %to 96 ;

    data temporary;

    set data_d&&d&i;

    run;

%end;

%mend MacroStandard;

 

So now what I wanted to do is for each of this dataset, check if one variable exists. So I have found a %Varexist(dataset, variable) macro, that perfectly works when I do

%put %Varexist(data_d01, myvar)

But I want to do that for each area, so I have tried several combinations. I have now one that is working, but I definitely do not understand why at all, as it works since I have commentated a line to see what was happening, but does not work if I remove the line which is supposed not to be read.

Here is my program :

%macro TestDep ();

data _null_;

set dep.dep2;

call symput('d'!!left(_n_),dep2);

run;

%do i=1 %to 96

%put d&&d&i;

*%put %VarExist(PotePrec.pote&anprec._d&&d&i,_1DX);

%end;

%mend TestDep;

%TestDep()

I would gladly appreciate some help

EDIT 1 : Add the macro varexist

%macro VarExist(ds,var);

  %local rc dsid result;

  %let dsid=%sysfunc(open(&ds));

  %if %sysfunc(varnum(&dsid,&var)) > 0 %then %do;

  %let result=1;

  %put NOTE: Var &var exists in &ds;

  %end;

  %else %do;

  %let result=0;

  %put NOTE: Var &var not exists in &ds;

  %end;

  %let rc=%sysfunc(close(&dsid));

  &result

%mend VarExist;


Accepted Solutions
Solution
‎10-14-2014 03:48 AM
Super User
Posts: 6,928

Re: Lost with macro calls

The %do i = 1 %to 96 misses the semicolon.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,928

Re: Lost with macro calls

See

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543665.htm

Comments of the * ...... ; form are still subject to macro processing.

Your line causes the varexist macro to be compiled&executed. Only /* ....  */ prevents macro execution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 24

Re: Lost with macro calls

That explains a lot thx, but not everything.

So my macro works as it is written, and works when the *%put %VarExist(PotePrec.pote&anprec._d&&d&i,_1DX); is not preceded by a *

But it does not work if I remove the previous %put line, and I do not understand why, as I assume -probably wrongly- it only does something external to the program

Solution
‎10-14-2014 03:48 AM
Super User
Posts: 6,928

Re: Lost with macro calls

The %do i = 1 %to 96 misses the semicolon.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 24

Re: Lost with macro calls

Well I am a bit confused, but thanks a lot, with your two answers I am now able to work appeased

Super User
Posts: 6,928

Re: Lost with macro calls

You're not alone; I had lots of confusion grasping the concepts of the macro processor. Sometimes I still have.

Not that much different from the C preprocessor or similar tools.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 305

Re: Lost with macro calls

Hello,

It would be helpful if you also list the macro Varexist

Contributor
Posts: 24

Re: Lost with macro calls

Added

Valued Guide
Posts: 3,208

Re: Lost with macro calls

Why not use the sashelp.vmembers access as being the same as some dictionary te quest?

---->-- ja karman --<-----
Contributor
Posts: 24

Re: Lost with macro calls

I would say for two very bad reasons

1. I have just arrived at my job and I tend to do thnigs the same way it has been done instead of recreating all myself

2. because I have a lot of trouble understanding your answer after "Why not use" (Im working a bit on that one)

Super User
Posts: 6,928

Re: Lost with macro calls

sashelp.vmember is a dynamic view that contains metadata about all datasets in all currently assigned libraries.

sashelp.vcolumn does the same for all columns in all tables in all currently assigned libraries.

Very useful.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 3,208

Re: Lost with macro calls

@Kurt even after many years I still having surprise with that macro environment.

Aboiron the set statement for concat all sasdatasets could be coded easier.

The set statement is supporting dataset lists. http://support.sas.com/documentation/cdl/en/lestmtsref/67407/HTML/default/viewer.htm#p00hxg3x8lwivcn...

You could code the range or as a abbreviation using :

---->-- ja karman --<-----
☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 306 views
  • 6 likes
  • 4 in conversation