DATA Step, Macro, Functions and more

%Do syntax trouble

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 83
Accepted Solution

%Do syntax trouble

[ Edited ]

Hello,

 

I have one large data set (P_Top) that I divide into smaller datasets based on variable POD. These smaller data sets are then sent to a directory as excel files. My problem is that I want to drop the POD variable from these data sets prior to exporting them but am running into issues doing that. I'm pretty much blanking out because I'm using POD as the variable in the macro to create them to begin with. I feel like I'm tripping over my self here. 

 

Appreciate any thoughts? Thanks. 

 

options mlogic mprint;
%macro split1 (data=, var=);

/*distinct field */
proc sort data = P_Top out = PDS nodupkey;
by POD;
run;

/* workbook variables */
data PDS;
set PDS;
POD1 = tranwrd(trim(POD),' ','_');
keep POD POD1;
run;

data _null_;
set PDS end = last;
call symputx ('val'||left(_n_),POD);
call symputx ('valx'||left(_n_),POD1);
if last then call symput('count',_n_);
run;


/* POD datasets */
data %do i = 1 %to &count;
&&valx&i %end;
;

set &data;
select(&var);
%do i = 1 %to &count;
when ("&&val&i") output &&valx&i;
%end;
otherwise;
end;
run;


%do i = 1 %to &count;
proc export
data = &&valx&i
outfile = "\\XD00040\&&valx&i"
DBMS = EXCEL replace;
SHEET = "&&valx&i";
run;
%end;

%mend split1;

%split1
(
data = P_Top,
var = POD
);

 


Accepted Solutions
Solution
‎11-07-2016 01:42 PM
Super User
Posts: 5,499

Re: %Do syntax trouble

There is a spot in the macro where you create the data sets.  That would be the proper point to drop the variable:

 

data %do i=1 %to &count;

            &&valx&i (drop=pod)

        %end;

         ;

View solution in original post


All Replies
Solution
‎11-07-2016 01:42 PM
Super User
Posts: 5,499

Re: %Do syntax trouble

There is a spot in the macro where you create the data sets.  That would be the proper point to drop the variable:

 

data %do i=1 %to &count;

            &&valx&i (drop=pod)

        %end;

         ;

Frequent Contributor
Frequent Contributor
Posts: 83

Re: %Do syntax trouble

Posted in reply to Astounding
I tried that so many different ways but the correct way. I didnt realize the correct syntax was to include the 'drop =' in parentheses.

Thanks for the help!



Super User
Posts: 11,343

Re: %Do syntax trouble

The drop could have been in several places. The data set option is one: data outputdatasetname (drop = variablelist) exclude the variables only from the specified set.

 or as a separate statement within the data step:   drop pod; run;  or: Set &data; drop pod;

Poor form but it would even work within a Select block as Drop like Format and label are not actually executeable statements.

would drop the selected variables from ANY data set built in the data step. Note this form does not use "="

☑ This topic is solved.

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

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