DATA Step, Macro, Functions and more

Import a text file, with a text.

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Import a text file, with a text.

Hi all,

 

I have a problem. So I wanted to do a macro which imports a txt file into sas. The txt contains random words in one line. So basically I want to do is that put them into one column. My method would do it, but it's keep saying ERROR.

The ERROR:

"NOTE: Invalid data for words in line 12 1-4.
12 down, 5
words=. _ERROR_=1 _N_=12" and so on.

 

Or it just doesn't show any words. Just a '.'

 

Here is my code:

%macro wordfreq(x);
data test.wrd;
infile "D:\Munka\&x..txt"
DLM=',';
input words;
run;
%mend;

%wordfreq(wrd);

The txt contains:
random, words, coming, up, right, now, here, random, words, here, coming, down, up, word, coming, random, way

Thank you for your help!

 

 ps.: My main tast is to do a freq macro which group the same words and show the frequency of each ones. Smiley Happy 


Accepted Solutions
Solution
‎05-10-2017 03:15 AM
Super User
Posts: 17,784

Re: Import a text file, with a text.

If it's all separated by commas have you tried using a trailing @@? 

 

Input word $ @@;

 

 

View solution in original post


All Replies
Solution
‎05-10-2017 03:15 AM
Super User
Posts: 17,784

Re: Import a text file, with a text.

If it's all separated by commas have you tried using a trailing @@? 

 

Input word $ @@;

 

 

Contributor
Posts: 21

Re: Import a text file, with a text.

Thank you all for your answer (sorry for this late reply), almost every answer was correct. Thank you again! Smiley Happy Smiley Wink

PROC Star
Posts: 288

Re: Import a text file, with a text.

I think what you want is the @@ modifier on your input statement, as follows:

 



data want;
infile datalines dsd;
input words $ @@;
datalines;
random, words, coming, up, right, now, here, random, words, here, coming, down, up, word, coming, random, way
;

SAS Super FREQ
Posts: 8,742

Re: Import a text file, with a text.

Hi:

  I'm not entirely use why you need a Macro program to do this. I think all you need to do is change your INPUT statement and use PROC FREQ.

cynthia

word_freq.png

PROC Star
Posts: 288

Re: Import a text file, with a text.

Since Cynthia is on here answering questions, I think I'll just get started on my actual job... Smiley Wink

Super Contributor
Posts: 408

Re: Import a text file, with a text.

Two things come to mind when looking at your code:

 

1) Your input statement defines WORD as numeric. Use a dollar sign to indicate a character.

2) Your file seems to have multiple valiues for WORD in a single line. Your code however reads just the first word in every line. Use a double trailing at sign to retain the record in the buffer until all words are collected:

filename t temp;
data _null_;
file t;
put 'random, words, coming, up, right, now, here, random, words, here, coming, down, up, word, coming, random, way';
run;

%macro wordfreq(x);
data wrd;
infile t DLM=',';
input words $ @@;
run;
%mend;

%wordfreq(wrd);

(I have put your data into a temp file for easier testing)

 

Hope this helps,

- Jan.

Super Contributor
Posts: 408

Re: Import a text file, with a text.

Haha so many similar and correct answers in a minute. Bet they have all replied (as I did) thinking to be the first :-)
☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 275 views
  • 6 likes
  • 5 in conversation