DATA Step, Macro, Functions and more

Separate data from one column to different others

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Separate data from one column to different others

Hello,

I've a sas-file with one column left (originally coming from a csv-file) but the column consists different words separated by "_". Depending on the line in the file there is a different amount of used words and a different words are used. I would like to make different columns from this entry so that each column consists only one word and only has content if the word is available in the original file.

So I have:

Content
DS
DS_TL_PA
PA_TL

and I would like:

DSTLPA
DS
DSTLPA
TLPA

Does someone has an idea which function I need to use for this or how I can do this?


Accepted Solutions
Solution
‎08-25-2014 09:15 AM
Super User
Posts: 9,681

Re: Separate data from one column to different others

data have;
input x $ ;
cards;
DS
DS_TL_PA
PA_TL
;
run;
data temp(drop=i x);
 set have;
 n+1;
 do i=1 to countw(x,'_');
  v=scan(x,i,'_');output;
 end;
run;
proc transpose data=temp out=want(drop=_:) ;
by n;
var v;
id v;
run;

Xia Keshan

View solution in original post


All Replies
Solution
‎08-25-2014 09:15 AM
Super User
Posts: 9,681

Re: Separate data from one column to different others

data have;
input x $ ;
cards;
DS
DS_TL_PA
PA_TL
;
run;
data temp(drop=i x);
 set have;
 n+1;
 do i=1 to countw(x,'_');
  v=scan(x,i,'_');output;
 end;
run;
proc transpose data=temp out=want(drop=_:) ;
by n;
var v;
id v;
run;

Xia Keshan

New Contributor
Posts: 4

Re: Separate data from one column to different others

Thanks a lot Ksharp!

Only one problem left: I have as answer the text containing DS, TL, PA and so on but also some answers containing "other" and then the explanation why the answer "other" was chosen. Is there are possibility to group them all together in one column? The exact text doesn't matter for this, only the fact that the text contains the word "other" (as I want to calculate the frequency of the different answers).

Super User
Posts: 9,681

Re: Separate data from one column to different others

OK . It would be better if you could post some sample data.

data have;
input x $40. ;
cards;
DS
DS_TL_PA
PA_TL
OTHER_WHATEVER
WHY_OTHER
;
run;
data temp(drop=i x);
 set have;
 length id v $ 40;
 n+1;
 if find(x,'OTHER','i') then do;
 id='OTHER'; v=x; output;
 end;
else do;
do i=1 to countw(x,'_');
  id=scan(x,i,'_');v=id;output;
 end;
end;
run;
proc transpose data=temp out=want(drop=_:) ;
by n;
var v;
id id;
run;

Xia Keshan

New Contributor
Posts: 4

Re: Separate data from one column to different others

Thanks, it worked (and even better than I expected as the complete text is saved)!

Super Contributor
Posts: 305

Re: Separate data from one column to different others

Hello,

Another solution:

data have;
input text $50.;
datalines;
DS_XYZ_KO
DS_TL_PA
PA_TL
SA_KO
;
run;

data int1;
set have nobs=last;
do i=1 to last while(scan(text,i,"_") ne "");
sepvar=scan(text,i,"_");
output;
end;
run;

proc sql noprint;
select distinct sepvar, sepvar into :allvars separated by " char(10)," ,  :arrvar separated by " " from int1;
create table int2 (&allvars char(10));
quit;

%put &arrvar;

data want(drop=text i j );
set have ;
array all{*} $20 &arrvar ;
do i=1 to count(text,"_")+1;
do until (vname(all{j}) = scan(text,i,"_"));
j+1;
end;
call vname(all{j},all{j});
j=0;
end;
run;

New Contributor
Posts: 4

Re: Separate data from one column to different others

Thanks Loko for your help!

Although I used Ksharp's answer as it was easier for me to understand...

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 559 views
  • 5 likes
  • 3 in conversation