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:
DS | TL | PA |
---|---|---|
DS | ||
DS | TL | PA |
TL | PA |
Does someone has an idea which function I need to use for this or how I can do this?
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
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
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).
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
Thanks, it worked (and even better than I expected as the complete text is saved)!
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;
Thanks Loko for your help!
Although I used Ksharp's answer as it was easier for me to understand...
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.