create macro variable from 2 rows of data

Accepted Solution Solved
Reply
Super Contributor
Posts: 398
Accepted Solution

create macro variable from 2 rows of data

Hello,

 

I have an issue were I have a table with 4 columns

Year, Sec, Tab, VName

2015, Test1, TableTemp, V1

2015, Test1, TableTemp, V2

2015, Test1, TableCheck, V3

2015, Test1, TableCheck, V4

 

I need to create multiple marcro variables so I can use them in a drop statement.

 

I need a macro that has "V1, V2" and one that has "V3, V4".   I then need to loop through my table list till I match one of the tables listed and then add the macro variable to the drop statement.

 

What would be the best approach?

 

Thank you

 

 


Accepted Solutions
Solution
‎10-07-2015 10:35 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: create macro variable from 2 rows of data

Sorry, can we take a step back here.  What do you need to do?  What do you have?  This to me sounds like you have a dataset which looks like this:

Year, Sec, Tab, VName

2015, Test1, TableTemp, V1

2015, Test1, TableTemp, V2

2015, Test1, TableCheck, V3

2015, Test1, TableCheck, V4

 

And you also have datasets TableTemp and TableCheck, and from those you want to drop V1-V2, V3-V4 respectively, yes?  If so then why not just do:

data _null_;
  set drop_information_dataset;
  by tab;
  if first.tab then call execute(cats('data ',tab,'; set ',tab,' (drop=',vname));
  else call execute(' '||vname);
  if last.tab then call execute('); run;');
run;
  

This will generate a datastep for each block of tab, and create a drop list within that datastep.  No macro variables needed.

View solution in original post


All Replies
Regular Contributor
Posts: 212

Re: create macro variable from 2 rows of data

[ Edited ]

Hi Jerry,

 

Sometimes it is best to use %do loops like this.

 

 

%macro test(num);
%do i = 1 %to #
%put var&&i;
%end;
%mend;

%test(3);

 

Now fill this with your program.

Hope this will help you, but need to change %put to %let to create the macros.

This will only print then to the SAS LOG.

 

You need to put the %do loop exactly where you need to run the loop 

 

%do loop ;

   drop  ;

%end;

 

Something similar to this.

 

Att

 

Solution
‎10-07-2015 10:35 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: create macro variable from 2 rows of data

Sorry, can we take a step back here.  What do you need to do?  What do you have?  This to me sounds like you have a dataset which looks like this:

Year, Sec, Tab, VName

2015, Test1, TableTemp, V1

2015, Test1, TableTemp, V2

2015, Test1, TableCheck, V3

2015, Test1, TableCheck, V4

 

And you also have datasets TableTemp and TableCheck, and from those you want to drop V1-V2, V3-V4 respectively, yes?  If so then why not just do:

data _null_;
  set drop_information_dataset;
  by tab;
  if first.tab then call execute(cats('data ',tab,'; set ',tab,' (drop=',vname));
  else call execute(' '||vname);
  if last.tab then call execute('); run;');
run;
  

This will generate a datastep for each block of tab, and create a drop list within that datastep.  No macro variables needed.

Super Contributor
Posts: 398

Re: create macro variable from 2 rows of data

Thank you so much for the replies.

 

RW that worked perfect.  I've never seen this approach.

 

Thanks

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 242 views
  • 0 likes
  • 3 in conversation