DATA Step, Macro, Functions and more

Macro Function ... Need Help

Reply
N/A
Posts: 0

Macro Function ... Need Help

%macro TestGet(MACROV,Data=lib.test);
%local dsid;
%let dsid=%sysfunc(OPEN(&Data(where=(NAME="&NAME"))));
%let dsid=%sysfunc(CLOSE(&dsid));
%mend TestGet;
________________________________________________

%macro hey;
%let service=%TestGet(can,data=lib.test);
%put &service;
%mend;

________________________________________________

My Dataset is:

id name
1 ali
2 veli
3 can
4 cin

I've written two pieces of macro procedures. First macro is to generalize of retrieving a data from a dataset. First parameter is the data to be looked up in the dataset,and the second is the dataset name. I've created a local macro variable to initialize the look-up data to that variable.
So in the second procedure, i am trying to get the look-up data in the dataset and try tp print it to the log.

But i am proably doing something wrong. Could you please help me.

Best Regards,
Can Akcan
Regular Contributor
Posts: 241

Re: Macro Function ... Need Help

Posted in reply to deleted_user
Super Contributor
Super Contributor
Posts: 365

Re: Macro Function ... Need Help

Posted in reply to deleted_user
Hello Cakcan,

I am not sure what you are going to acheve. If you would like to test a succes of opening and closing a dataset then the following change to your code could help:

data a;
input id name $;
datalines;
1 ali
2 veli
3 can
4 cin
run;

options symbolgen mprint;

%macro TestGet(name,Data=lib.test);
%global dsid dsid1;
%let dsid=%sysfunc(OPEN(&Data(where=(NAME="&NAME"))));
%let dsid1=%sysfunc(CLOSE(&dsid));
%mend TestGet;

%macro hey;
%TestGet(name,data=work.a);
%put dsid=&dsid dsid1=&dsid1;
%mend;

%hey

Sincerely,
SPR
N/A
Posts: 0

Re: Macro Function ... Need Help

Posted in reply to deleted_user
my aim is to open a dataset, assign variable to a macro variable in the dataset and close it and then finally i have a generalized function which i write the name of the dataset and the column name and retieve the data from the that dataset. thats it. I hope it is clear.
Super Contributor
Super Contributor
Posts: 365

Re: Macro Function ... Need Help

Posted in reply to deleted_user
This my second attempt to understand what you need. The code below creates a dataset _a that contains the observation from input dataset for name="can". Is this what you need? or you need only ID for "can"?

%macro TestGet(name,Data=lib.test);
%local dsid;
%let dsid=%sysfunc(OPEN(&Data(where=(NAME="&NAME"))));
%if &dsid NE 0 %then %do;
data _a;
set &Data(where=(NAME="&NAME"));
run;
%end;
%else %put Error in &data;
%let dsid=%sysfunc(CLOSE(&dsid));
%mend TestGet;

%macro hey;
%TestGet(can,data=work.a);
proc print data=_a;
run;
%mend;

%hey
Valued Guide
Posts: 634

Re: Macro Function ... Need Help

Cakcan,
Notice then name change of the positional parameter in SPR's code. But I am worried about your statement of the problem. We are a bit confused because macro variables are not, in any way, assigned to data sets. You do not need to open a data set to create a macro variable. Macro variables are held in memory for the duration of the SAS job or session. The values of macro variables can be used in the process of data set variable creation and assignment, but that is a very different process than the one you have started here.
Art
N/A
Posts: 0

Re: Macro Function ... Need Help

Posted in reply to deleted_user
Thank you for your comments. I've got to the point where i want to achieve.
Thanx a lot.

Best Regards,
Cakcan
Ask a Question
Discussion stats
  • 6 replies
  • 149 views
  • 0 likes
  • 4 in conversation