BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aboiron
Calcite | Level 5

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;

1 ACCEPTED SOLUTION
11 REPLIES 11
Aboiron
Calcite | Level 5

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

Aboiron
Calcite | Level 5

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

Kurt_Bremser
Super User

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.

Loko
Barite | Level 11

Hello,

It would be helpful if you also list the macro Varexist

Aboiron
Calcite | Level 5

Added

jakarman
Barite | Level 11

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

---->-- ja karman --<-----
Aboiron
Calcite | Level 5

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)

Kurt_Bremser
Super User

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.

jakarman
Barite | Level 11

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1306 views
  • 6 likes
  • 4 in conversation