BookmarkSubscribeRSS Feed
France
Quartz | Level 8

dear all,

 

how can I split a  data based on the different values in a variable?

 

for example, I have a data 

 

person_ctry_code, company_name
US,A
UK,B
GB,C
NZ,D

how could I split it into three datasets? one is 

person_ctry_code, company_name
US,A

, second is

person_ctry_code, company_name
GB,B
GB,C

 third is,

person_ctry_code, company_name
NZ,D

 

thanks in advance.

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You would not want to.  This would mean 3 * storage needs (e.g. 3 * header blocks, 3 * read, 3 * write etc.), not to mention you would then need masses of complicated messy code which will fall over each time.  Never split data up unless there is an extremely good reason (i.e. if there is a restriction on the recipient end, or data is huge > tb).  

As for how, you would:

proc sort data=have out=loop nodupkey;
  by company_name;
run;
data _null_:
  set loop;
  call execute(cat('data ',strip(company_name),'; set have; where company_name="',strip(company_name),'"; run;'));
run;
PeterClemmensen
Tourmaline | Level 20

First: Don't do this. Usually it is a bad idea to split up a data set like this. Use By Group processing instead.

 

Secondly, there are several ways to do this. Here is a hash object approach.

 

data have;
input person_ctry_code $ company_name $;
infile datalines dlm=",";
datalines;
US,A
GB,B
GB,C
NZ,D
;

proc sort data = have out=SortHave;
   by person_ctry_code;
run;
 
data _null_;
   if _n_=1 then do;
      if 0 then set SortHave;
      declare hash h(dataset:"SortHave(obs=0)", multidata:'y');
      h.definekey(all:'y');
      h.definedata(all:'y');
      h.definedone();
   end;
   do until(last.person_ctry_code);                                     
      set SortHave;
      by person_ctry_code;
      h.add();
   end;
   h.output(dataset:person_ctry_code);
   h.clear();
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 558 views
  • 1 like
  • 3 in conversation