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

Split data conditionally

Reply
Frequent Contributor
Posts: 84

Split data conditionally

I have multiple data set and using following program I have split data according to variable Siml

data _null_;                                                                                                                                                                                                                                                   

     declare hash h();                                                                                                                                                                                                                                         

     h.definekey('_n_');                                                                                                                                                                                                                                       

     h.definedata('Siml', 'Subj', 'Seq', 'P','Reg', 'LogAuc', 'LogCmax');                                                                                                                                                                                      

     h.definedone();                                                                                                                                                                                                                                           

     do _n_=1 by 1 until (last.Siml);                                                                                                                                                                                                                          

           set analysis;                                                                                                                                                                                                                                       

           by Siml;                                                                                                                                                                                                                                            

           rc=h.replace();                                                                                                                                                                                                                                     

     end;                                                                                                                                                                                                                                                      

     rc=h.output(dataset:cats('want',Siml));                                                                                                                                                                                                                   

run;

Now I want to use these split data set in my further analysis for this I have written following macro code that is working fine but i want to do this without macro Plz help

%macro split;                                                                                                                                                                                                                                                  

data finall;                                                                                                                                                                                                                                                   

set                                                                                                                                                                                                                                                            

%do i = 1 %to 5;                                                                                                                                                                                                                                               

want&i;                                                                                                                                                                                                                                                        

run;                                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                               

proc print data=want&i;                                                                                                                                                                                                                                        

run;                                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                               

%end;                                                                                                                                                                                                                                                          

;                                                                                                                                                                                                                                                              

run;                                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                               

%mend;

Super User
Super User
Posts: 7,401

Re: Split data conditionally

Lets jump back a moment here.  Start by posting an example (in the form of a datastep) and and example of what you want out.   I see no reason in the above code why you would be using hash code, nor why you would want to split data in the first place.  There is probably a very simple process for what you want to do, however I cannot see it from what you have posted.

Frequent Contributor
Posts: 84

Re: Split data conditionally

Dear RW9

I have a data set in which there are some variables like Siml, Seq, P,,,,,,,,

Now I want to split this data set according to variable Siml for this I am using following code and its work fine

data _null_;

     declare hash h();

     h.definekey('_n_');

     h.definedata('Siml', 'Subj', 'Seq', 'P','Reg', 'LogAuc', 'LogCmax');

     h.definedone();

     do _n_=1 by 1 until (last.Siml);

           set complete;

           by Siml;

           rc=h.replace();

     end;

rc=h.output(dataset:cats('want',Siml));

run;

Now I want to use these split data set in my further analysis for this I have written following macro code that is working fine but i want to do this without macro Plz help


%macro split;

data finall;

set

%do i = 1 %to 5;

want&i;

run;

proc print data=want&i;

run;

%end;

;

run;

   %mend;


Super User
Super User
Posts: 7,401

Re: Split data conditionally

Yes, but you haven't answered the question.  Why do all that code to split a dataset - which in itself is not a recommended approach - when simpler code might make the data easier to use.  After splitting it then you just seem to be putting it back together again.  Just doesn't make sense.  If you don't want to use macro then:

data _null_;

     call execute('data final; set ');

     do i=1 to 5;

          call execute(' want'||strip(put(i,best.)));

     end;

     call execute('; run;');

run;

However it is really doing the same thing at the end of the day.

Also, if they are all called the same name then you can use the colon modifier:

data tmp1;

  a=1;

run;

data tmp2;

  a=2;

run;

data want;

  set tmp:;

run;

Frequent Contributor
Posts: 84

Re: Split data conditionally

Thank you very much for your reply I tried your code and its working fine thank you again

There is thing that is bothering me if I want to print split data in ever do loop as I did in macro program so what command should I add in your first code of call execute

Regards

Frequent Contributor
Posts: 84

Re: Split data conditionally

And is there any option by which I can print these split data sets using do loop?

Super User
Super User
Posts: 7,401

Re: Split data conditionally

Well, yes:

data _null_;

     call execute('data final; set ');

     do i=1 to 5;

          call execute(' want'||strip(put(i,best.)));

     end;

     call execute('; run;  proc print data=want'||strip(put(i,best.))||'; run;');

run;

However, if the only reason you want to split them is to print them separately, then look at by group processing.  You just need one variable in your dataset, and then you can do:

proc print data=want;

     by id_var;

run;

And if you want further options move to proc report, then you can do page by, groupings, computed items etc.  That would be my suggestion.  It really looks like your coding all of this just to avoid using proc report which doesn't make sense as that is a very powerful reporting environment.

Ask a Question
Discussion stats
  • 6 replies
  • 486 views
  • 0 likes
  • 2 in conversation