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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

Input word $ @@;

 

 

View solution in original post

7 REPLIES 7
Reeza
Super User

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

 

Input word $ @@;

 

 

Derdavos
Obsidian | Level 7

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

collinelliot
Barite | Level 11

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
;

Cynthia_sas
SAS Super FREQ

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

collinelliot
Barite | Level 11

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

jklaverstijn
Rhodochrosite | Level 12

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.

jklaverstijn
Rhodochrosite | Level 12
Haha so many similar and correct answers in a minute. Bet they have all replied (as I did) thinking to be the first 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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