BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
annelies_wiv
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

6 REPLIES 6
Ksharp
Super User
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

annelies_wiv
Calcite | Level 5

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).

Ksharp
Super User

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

annelies_wiv
Calcite | Level 5

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

Loko
Barite | Level 11

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;

annelies_wiv
Calcite | Level 5

Thanks Loko for your help!

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1352 views
  • 5 likes
  • 3 in conversation