BookmarkSubscribeRSS Feed
cody_q
Calcite | Level 5

Hey all,

I would like to seek some help and solutions through this discussion thread.
Currently, i am working on a column with the content for teacher's comment.

Then i would like to delimit it by the space or comma or punctuation marks between words inside the content.

At the same time , i would like to take out each of the words in the content and then place them into rows instead of columns .

Example .
Catherine is a good girl . -> This is the teacher's comment.

After delimit and placing them into rows in SAS dataset , it would look like this in 5 rows :
Catherine
is

a

good

girl

Thank you all .

Regards,
Cody

7 REPLIES 7
QLi
Fluorite | Level 6 QLi
Fluorite | Level 6

If you use Enterprise Guide, you can follow these steps.

From" File" in the tool Bar , select "Import Data", 

Click "Text Format "  , select "Delimited" in the format and select "Space" in the  Delimiter

Click Run.

Try it and good luck

Doc_Duke
Rhodochrosite | Level 12

Cody,

QLi's approach should put each word into its own variable (you may need to add the MISSOVER option to allow for different number of words in each comment).  You can then use the transpose task (tasks --> data --> transpose) to get the words into rows.

Doc Muhlbaier

Duke

cody_q
Calcite | Level 5

Hi Doc Muhlbaier and QLi,

After putting each word into its own variable, how do I put the few rows into one column instead?

a     b     c

d     e     f

g     h     i

to

a

b

c

d

e

f

g

h

i

Thanks

shivas
Pyrite | Level 9

Hi,

Try this..

data one;

input comments $ :15. @@;

cards;

Catherine is a good girl

;

run;

data two;

input one $ two $ three $;

cards;

a     b     c

d     e     f

g     h     i

;

run;

data want;

set two;

array xx[3] one two three;

do i=1 to dim(xx);

if xx ne '' then yy=xx;

output;

end;

drop one--three i;

run;

Thanks,

Shiva

cody_q
Calcite | Level 5

Hi Shivas,


Thanks for your reply. I have tried it and it worked.
However, my concern is that the data which I am using is of a large scale (2000 of values)
Do you have any way which I could work around it ?
Is there a way to import the file which it will automatically change to the 1 column , many rows output.

Thank you.

Regards,
Cody

cody_q
Calcite | Level 5

Hi Shivas,


Thanks for your reply. I have tried it and it worked.
However, my concern is that the data which I am using is of a large scale (2000 of values)
Do you have any way which I could work around it ?
Is there a way to import the file which it will automatically change to the 1 column , many rows output.

Thank you.

Regards,
Cody

p12937
Obsidian | Level 7

How about something like this:

filename in_file 'your_text_file' lrecl=32767;

/* split words separated by spaces */
data in_file;
infile in_file length=l;
length word_nospace $50.;
keep word_nospace;
  input record $varying32767. l;
  delims=count(trim(record), ' ')+1;
  do i=1 to delims;
   word_nospace=scan(record, i, ' ');
   output;
  end;
run;

/* split words separated by commas */
data in_file;
set in_file;
keep word_nocomma;
  delims=count(word_nospace, ',')+1;
  do i=1 to delims;
   word_nocomma=scan(word_nospace, i, ',');
   if not missing(word_nocomma) then output;
  end;
run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 1375 views
  • 9 likes
  • 5 in conversation