DATA Step, Macro, Functions and more

How to use observations of one dataset as macro variables in another program?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

How to use observations of one dataset as macro variables in another program?

Hi Mate,

 

I need help from you guys. Let me explain you my problem firtst. I have created a single variable dataset (All the observation of this dataset are variables for my other programming.). Now i need to use the observations in a sequence like 1st observation and 2nd observation will be considered as Macro variables "START" and "STOP" in another programe and once the macro finishes forst ittration then 2nd observation should be "START" and 3rd observation should be "STOP" variable. Like this, the entire observations need to be used as derived Macro variables in another programme.

 

Example:

Dataset_1

obs Title

01 Name

02 age

03 sex

04 address

05 language

 

Programme:

 

%macro test (Start= , Stop= );

if var1= &START then Seq+1;

if var1= &STOP then seq+1;

if seq=1 or var1= &STOP;

%mend test;

 

Please provide me a solution to use the observation of dataset_1 as macro variables in the above programe in the above specified sequence.

 

Thanks

 

 


Accepted Solutions
Solution
‎07-11-2017 03:34 AM
PROC Star
Posts: 1,555

Re: How to use observations of one dataset as macro variables in another program?

Or like this?


data _null_;
  set HAVE;
  PREV=lag(VAR);
  if PREV ne ' ' then 
  call execute('%test(start='||PREV||',stop='||VAR||');');
run;

 

View solution in original post


All Replies
Super User
Posts: 17,737

Re: How to use observations of one dataset as macro variables in another program?

Look at CALL EXECUTE if I understand your question correctly. It's ambiguous. 

 

The example in the documentation shows how a dataset can drive a macro. 

PROC Star
Posts: 1,555

Re: How to use observations of one dataset as macro variables in another program?

Like this?

data HAVE;
input VAR $;
cards;
Name
age
sex
address
language
run;
proc sql noprint;
  select VAR into :varlist separated by ' ' from HAVE;
quit;

%macro test(start=, stop=);
  %put Replace with actual code &=start &=stop;
%mend;

%macro loop;
   %local i;
    %do i=1 %to &sqlobs.-1;
      %test(START=%scan(&varlist, &i),STOP=%scan(&varlist, &i+1));
  %end;
%mend;
%loop;

Replace with actual code START=Name STOP=age
Replace with actual code START=age STOP=sex
Replace with actual code START=sex STOP=address
Replace with actual code START=address STOP=language

 

 

Solution
‎07-11-2017 03:34 AM
PROC Star
Posts: 1,555

Re: How to use observations of one dataset as macro variables in another program?

Or like this?


data _null_;
  set HAVE;
  PREV=lag(VAR);
  if PREV ne ' ' then 
  call execute('%test(start='||PREV||',stop='||VAR||');');
run;

 

Occasional Contributor
Posts: 8

Re: How to use observations of one dataset as macro variables in another program?

The programme provided is working only for numeric variables. Can you please modify it to work for character variable as well?

PROC Star
Posts: 1,555

Re: How to use observations of one dataset as macro variables in another program?

How is "Name" a numeric?
Occasional Contributor
Posts: 8

Re: How to use observations of one dataset as macro variables in another program?

the programe is not working for the variables like name, address and language.

PROC Star
Posts: 1,555

Re: How to use observations of one dataset as macro variables in another program?

It is and I even provided the outputs
Occasional Contributor
Posts: 8

Re: How to use observations of one dataset as macro variables in another program?

Thank you for your help Chriz. Now i got it.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 98 views
  • 3 likes
  • 3 in conversation