SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Data management

Reply
Frequent Contributor
Posts: 84

Data management

I have two data sets named want1 want2

ObsSCABMIBC
1119.71163.31063.1723821.5392
2119.64322.654162.1335920.1039

ObsSCABMIBC
1221.11722.413811.8241821.1483
2226.50521.413811.5013825.7135

Now i am using following program for my calculation that is working for want1 but giving some error for want2

%macro con;                                                                                                                                                                                                                                            

data final2;                                                                                                                                                                                                                                                   

set                                                                                                                                                                                                                                                            

%do i = 1 %to 2;                                                                                                                                                                                                                                               

want&i;                                                                                                                                                                                                                                                    

if _n_ = round((0.95*2)) then output;                                                                                                                                                                                                                          

run;                                                                                                                                                                                                                                                          

%end;                                                                                                                                                                                                                                                          

;                                                                                                                                                                                                                                                              

run;                                                                                                                                                                                                                                                          

Proc print data=final2;                                                                                                                                                                                                                                        

run;                                                                                                                                                                                                                                                          

%mend;                                                                                                                                                                                                                                                   

%con;

The log is

NOTE: There were 2 observations read from the data set WORK.WANT1.

NOTE: The data set WORK.FINAL2 has 1 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.05 seconds

      cpu time            0.00 seconds

NOTE: Line generated by the invoked macro "CON".

28766           if _n_ = round((0.95*2)) then output;

                --

                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "I".

28766   want2

        -----

        180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: There were 1 observations read from the data set WORK.FINAL2.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.06 seconds

      cpu time            0.00 seconds

Kindly help me in this regard and point out where actually the error is

Regards

Super User
Super User
Posts: 7,407

Re: Data management

Sorry, that code is a bit of a mess.  The reason you are getting the error is because of the macro loop, imagine what the code being generated looks like:

    %do i = 1 %to 2;                       

      want&i;                   

      if _n_ = round((0.95*2)) then output;

      run;                             

    %end;                                                                                                                                                                                                                   

So set is there, then the first loop occurs:

data final2; set want1.; if _n_=round((0.85*2)) then output; run;

Then the next i loop occurs:

data final2; set want1.; if _n_=round((0.85*2)) then output; run; want2; if _n_=round((0.95*2)) then output; run;

As you can see the second loop has no data final2 or set and so becomes invalid code. 


Of course, as is almost always the case, you don't need any of that macro code at all, the semicolon modifier on the dataset name can be used:

data final2;

     set want:;

     if _n_=round((0.95*2)) then output;

run;

Frequent Contributor
Posts: 84

Re: Data management

Thank RW9

Actually I want to apply if condition on each of the separate data sets want1 (two obs) and want2 (two obs)

According to your code of semicolon modifier, it actually combine both data sets and then apply if condition on combined data sets (with 4 obs)

Super User
Super User
Posts: 7,407

Re: Data management

So what is the condition, it looks like your using the _n_ automatic obs number, it isn't advised.  As for the other part:

data _null_:

     do i=1 to 2;

          call execute(cats('data final',put(i,1.),'; set want',put(i,1.),'; if _n_=round((0.95*2)) then output; run;'));

     end;

run;

Super User
Posts: 17,863

Re: Data management

Look at the INDSNAME option which allows you to identify the source for an observation when data is appended.

Also, there's the IN option in the SET statement that will identify the source, but that doesn't work well with the colon modifier.

Once you've identified the source you can apply your conditional logic.

data final2;

set want: indsname=source;

input=source;

run;

Valued Guide
Posts: 858

Re: Data management

I cannot tell what you are trying to accomplish with this code.  I would be sure to run what you intend without the macro first, then run the macro and verify the output.  This might be what you are intending to do but I'm not sure:

%macro con;                                                                                                                                                                                                                                      
data final2;                                                                                                                                                                                                                                             
%do i = 1 %to 2;                                                                                                                                                                                                                                         
set want&i;                                                                                                                                                                                                                                                                                                                                     

%end;

if _n_ = round((0.95*2)) then output;                                                                                                                                                                                                                    

run; 

;                                                                                                                                                                                                                                                        
run;                                                                                                                                                                                                                                            
%mend;                                                                                                                                                                                                                                             

%con;

Super User
Posts: 10,516

Re: Data management

And really what is the difference between

if _n_ = round((0.95*2)) then

and

If _n_ = 2 then

other than verbosity and almost certainly more cpu operations?

round((0.95*2)) always results in 2, so why bother???

Ask a Question
Discussion stats
  • 6 replies
  • 455 views
  • 0 likes
  • 5 in conversation