DATA Step, Macro, Functions and more

Reg:Sort Data in the same observation based on comma

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Reg:Sort Data in the same observation based on comma

Hi i am having data like this

data Temp;
input name $ 1-30;
cards;
cell,van,zim
zim,app,Run
Mor,App,hen
zac,zil,run,fun,app,box,cat

run;

But i want the output in the sort order i dont know how many obs with comma will come

output

cell,van,zim
app,run,zim
app,hen,mor
app,box,cat,fun,run,zac,zil


Accepted Solutions
Solution
‎05-11-2012 03:35 AM
Super User
Posts: 10,023

Re: Reg:Sort Data in the same observation based on comma

OK. Let me try it.

data Temp;
input name $ 1-30;
cards;
cell,van,zim
zim,app,Run
Mor,App,hen
zac,zil,run,fun,app,box,cat
zac,bil
;
run;
proc sql noprint;
select max(count(name,','))+1 into : max from temp;
quit;

data want;
 set temp;
 array _a{&max} $ ;
 do i=1 to &max;
  _a{i}=lowcase(scan(name,i,','));
 end;
 call sortc(of _a{*});
 name=catx(',',of _a{*});
 drop i _a:;
run;


Ksharp

View solution in original post


All Replies
Solution
‎05-11-2012 03:35 AM
Super User
Posts: 10,023

Re: Reg:Sort Data in the same observation based on comma

OK. Let me try it.

data Temp;
input name $ 1-30;
cards;
cell,van,zim
zim,app,Run
Mor,App,hen
zac,zil,run,fun,app,box,cat
zac,bil
;
run;
proc sql noprint;
select max(count(name,','))+1 into : max from temp;
quit;

data want;
 set temp;
 array _a{&max} $ ;
 do i=1 to &max;
  _a{i}=lowcase(scan(name,i,','));
 end;
 call sortc(of _a{*});
 name=catx(',',of _a{*});
 drop i _a:;
run;


Ksharp

Contributor
Posts: 37

Re: Reg:Sort Data in the same observation based on comma

Thqs Ksharp

Frequent Contributor
Posts: 140

Re: Reg:Sort Data in the same observation based on comma

Hi khsarp some times i will run my job with 0 records when i do this i am getting Error what can i do

as we run in DI and we run this job daily some times we get o kb file then also we run the job then job fails

what can i do for this.

data Temp;

input name $ 1-30;

cards;

;

run;

proc sql noprint;

select max(count(name,','))+1 into : max from temp;

quit;

data want;

set temp;

array _a{&max} $ ;

do i=1 to &max;

  _a{i}=lowcase(scan(name,i,','));

end;

call sortc(of _a{*});

name=catx(',',of _a{*});

drop i _a:;

run;

Super User
Posts: 10,023

Re: Reg:Sort Data in the same observation based on comma

Posted in reply to sas_Forum
data Temp;
input name $ 1-30;
cards;
;
run;



%macro sort_char;
%let dsid=%sysfunc(open(temp));
%let nobs=%sysfunc(attrn(&dsid,nobs));
%let dsid=%sysfunc(close(&dsid));
%put &nobs ;
%if &nobs=0 %then %return;
proc sql noprint;
select max(count(name,','))+1 into : max from temp;
quit;

data want;
 set temp;
 array _a{&max} $ ;
 do i=1 to &max;
  _a{i}=lowcase(scan(name,i,','));
 end;
 call sortc(of _a{*});
 name=catx(',',of _a{*});
 drop i _a:;
run;
%mend sort_char;

%sort_char






Ksharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 218 views
  • 0 likes
  • 3 in conversation