<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Dictionary analysis of a dataset of tweets in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688588#M24614</link>
    <description>&lt;P&gt;Hi friends,&lt;/P&gt;
&lt;P&gt;I have a dataset which includes the content of tweets of a sample of firms.&lt;/P&gt;
&lt;P&gt;I also have an MS Word file which is a dictionary of words. The Word file is a normal file and the words in it are separated by comma.&lt;/P&gt;
&lt;P&gt;I want to count the number of times the words of the dictionary appear in each tweet.&lt;/P&gt;
&lt;P&gt;I am pretty new to SAS and do not know programming. Is there any straightforward way of doing this in SAS?&lt;/P&gt;
&lt;P&gt;Thanks so much in advance.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Oct 2020 18:10:49 GMT</pubDate>
    <dc:creator>AlG</dc:creator>
    <dc:date>2020-10-02T18:10:49Z</dc:date>
    <item>
      <title>Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688588#M24614</link>
      <description>&lt;P&gt;Hi friends,&lt;/P&gt;
&lt;P&gt;I have a dataset which includes the content of tweets of a sample of firms.&lt;/P&gt;
&lt;P&gt;I also have an MS Word file which is a dictionary of words. The Word file is a normal file and the words in it are separated by comma.&lt;/P&gt;
&lt;P&gt;I want to count the number of times the words of the dictionary appear in each tweet.&lt;/P&gt;
&lt;P&gt;I am pretty new to SAS and do not know programming. Is there any straightforward way of doing this in SAS?&lt;/P&gt;
&lt;P&gt;Thanks so much in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 18:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688588#M24614</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-10-02T18:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688607#M24616</link>
      <description>&lt;P&gt;1. Get your data into SAS. First figure out how to import your tweets and then figure out how to get your data from Word into SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Separate each set of data into words&lt;/P&gt;
&lt;P&gt;3. Filter your big list based on your look up list&lt;/P&gt;
&lt;P&gt;4. Do the counts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first here shows how I input a set of statements that are manually and then separate them into words.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data random_sentences;
    infile cards truncover;
    informat sentence $256.;
    input sentence $256.;
    cards;
This is a random sentence
This is another random sentence
Happy Birthday
My job sucks.
This is a good idea, not.
This is an awesome idea!
How are you today?
Does this make sense?
Have a great day!
;;;;

*Partition into words;
data f1;
    set random_sentences;
    id=_n_;
    nwords=countw(sentence);
    nchar=length(compress(sentence));

    do word_order=1 to nwords;
        word=scan(sentence, word_order);
        output;
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315691"&gt;@AlG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi friends,&lt;/P&gt;
&lt;P&gt;I have a dataset which includes the content of tweets of a sample of firms.&lt;/P&gt;
&lt;P&gt;I also have an MS Word file which is a dictionary of words. The Word file is a normal file and the words in it are separated by comma.&lt;/P&gt;
&lt;P&gt;I want to count the number of times the words of the dictionary appear in each tweet.&lt;/P&gt;
&lt;P&gt;I am pretty new to SAS and do not know programming. Is there any straightforward way of doing this in SAS?&lt;/P&gt;
&lt;P&gt;Thanks so much in advance.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 19:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688607#M24616</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-02T19:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688615#M24617</link>
      <description>&lt;P&gt;One thing you will want to do is save the Word document as plain text. That way you have a chance of reading into SAS. The Word file format has a LOT of stuff that you do not want related to fonts and text appearance, bookmarks, style settings ad nauseum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good idea to provide more than generic suggestions as above is to provide a couple of small examples of each type of data. Then show how you expect the desired output to appear for that example. The examples should provide different types of things to count such as how to count the same word appearing multiple times in one tweet, are plurals supposed to be counted separately (cow and cows: one word or two separate). One consideration is to look closely for compound words and either of the parts of a compound appearing separately and how components might need to be treated. Example: Streetcar, car and street. Do you want "street" or "car" to have a count if the word "streetcar" appears in a tweet? Or how about someone censoring a word like s%%t in a tweet. Do you expect that to match anything?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the whole hashtag nonsense means that those can contain many "words". So include if preceded by # or not?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 19:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688615#M24617</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-02T19:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688787#M24628</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;Thanks for your response. As I said, I am pretty amateur so I am trying to make sense of what you said. I have a few questions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Assuming that the tweets are already in a CSV file I guess I don't need to do the first part of your code (the whole part leading to "*Partition into words;". Correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Based on what you said, I need to decompose each tweet into words. Correct? If so, let's say the tweets are in a column titled "tweets". Should I modify your code in the following manner? (I have just replaced the word "sentence" in your code with "tweets", which is the column which includes my tweets).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;data f1;
    set Twitter;
    id=_n_;
    nwords=countw(Tweets);
    nchar=length(compress(Tweets));

    do word_order=1 to nwords;
        word=scan(Tweets, word_order);
        output;
    end;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Oct 2020 19:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688787#M24628</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-10-04T19:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688789#M24629</link>
      <description>&lt;P&gt;Thanks so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;. I just want to do a quick and dirty thing, so I don't care about word stems, etc.&lt;/P&gt;
&lt;P&gt;Not sure how to provide a sample of my data here, I know it is not a good idea to attach a file. Does what I have included below work?&lt;/P&gt;
&lt;P&gt;Let's say, I have a CSV file named "firm tweet". It just includes a column titled "tweets". Below is a sample:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Tweets&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Years of piano lessons finally paying off. &lt;A href="http://t.co/yti347HqXh" target="_blank" rel="noopener"&gt;http://t.co/yti347HqXh&lt;/A&gt; &lt;A href="http://t.co/0R3jRfxRCv" target="_blank" rel="noopener"&gt;http://t.co/0R3jRfxRCv&lt;/A&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;10 hours until puck drop. #AnythingForHockey &lt;A href="http://t.co/85ObrRCmUN" target="_blank" rel="noopener"&gt;http://t.co/85ObrRCmUN&lt;/A&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Dawn of the Lager. MT @michael_hinshaw: I think if the zombie apocalypse began, Yuengling would be the new currency.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I have a text file named "dictionary". It has just one line, which is below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;compensat*, offer*, recover*, refund*, reimburs*, repay*, restor*, return*&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said I just want to quickly check how many times (if any), each of these words appear, in any form (e.g., plural, singluar), in each tweet.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Oct 2020 19:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688789#M24629</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-10-04T19:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688809#M24630</link>
      <description>That's correct. Then you do the same for your lookup file. Then it becomes very easy to do the calculation you need. There are other ways that are faster but this is the easiest way for a beginner to understand it IMO. &lt;BR /&gt;&lt;BR /&gt;Once you do that for both sets, you can figure out which words are in both via a merge. &lt;BR /&gt;&lt;BR /&gt;data matched;&lt;BR /&gt;merge tweets (in=intweet) lookup (in=inlookup);&lt;BR /&gt;by words;&lt;BR /&gt;if not inlookup then count=1; else count=1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Everything with a 1 is now matched and you can then count/add them up per sentence or do whatever you want. &lt;BR /&gt;&lt;BR /&gt;A different, more complicated method, is to load the lookup words into a temporary array. Rather than output the words in the step above you'd simply count how many matched as you looped through and then at the end of that single step you'd have your count. But that requires more complicated programming. &lt;BR /&gt;&lt;BR /&gt;By the way, this is much easier if you have SAS Text Analytics tool - that likely has different features that would make this infinitely easier but this is the 'brute' force way that works with any tool and in any language.</description>
      <pubDate>Mon, 05 Oct 2020 01:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688809#M24630</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-05T01:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dictionary analysis of a dataset of tweets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688822#M24631</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315691"&gt;@AlG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;. I just want to do a quick and dirty thing, so I don't care about word stems, etc.&lt;/P&gt;
&lt;P&gt;Not sure how to provide a sample of my data here, I know it is not a good idea to attach a file. Does what I have included below work?&lt;/P&gt;
&lt;P&gt;Let's say, I have a CSV file named "firm tweet". It just includes a column titled "tweets". Below is a sample:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Tweets&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Years of piano lessons finally paying off. &lt;A href="http://t.co/yti347HqXh" target="_blank" rel="noopener"&gt;http://t.co/yti347HqXh&lt;/A&gt; &lt;A href="http://t.co/0R3jRfxRCv" target="_blank" rel="noopener"&gt;http://t.co/0R3jRfxRCv&lt;/A&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;10 hours until puck drop. #AnythingForHockey &lt;A href="http://t.co/85ObrRCmUN" target="_blank" rel="noopener"&gt;http://t.co/85ObrRCmUN&lt;/A&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="100%" height="30px"&gt;Dawn of the Lager. MT @michael_hinshaw: I think if the zombie apocalypse began, Yuengling would be the new currency.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I have a text file named "dictionary". It has just one line, which is below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;compensat*, offer*, recover*, refund*, reimburs*, repay*, restor*, return*&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said I just want to quickly check how many times (if any), each of these words appear, in any form (e.g., plural, singluar), in each tweet.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since none of the examples appear in the "data" and no example of how the count is to be presented that is kind of problematic as far as an example goes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your "*" at the end of the "word" mean "match anything that starts with" the word? If that is the case they yes to do care about word stems because you are specifying one. If "compensat*" is to consider "compensate" "compensation" "compensatory" and "compensates" as matches then "compensat" is indeed a stem&lt;/P&gt;
&lt;P&gt;The index function will find any substring. The function returns a position number in a string if found or 0 otherwise. So a counter could be incremented when the position is &amp;gt;0.&lt;/P&gt;
&lt;P&gt;An example of the function placing a count for each "word" into a separate variable per record:&lt;/P&gt;
&lt;PRE&gt;data example;
   X ="This string contains several strings that could be severely searched with various string functions.";
   /* a temporary variable to hold one word from the long string
      at a time since the apparent search comparison is a 
      "starts with"
   */
   length temp $ 25; 
   array words {3} $ 25 _temporary_ ("string","sever","sea");
   array c string_count sever_count sea_count;
   do k= 1 to countw(x);
      temp=upcase(scan(x,k));
      do i=1 to dim(words);
         c[i]=sum(c[i],index(strip(temp),strip(upcase(words[i])))&amp;gt;0);
      end;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;The Strip function is used to remove blanks, otherwise any potential leading or trailing blank is used in the comparison. Upper case, optionally lower case, so that you don't have to provide values to search for the upper / lower/ mixed case versions of the "words". Notice that the temporary array holds the values to search for and does &lt;STRONG&gt;not&lt;/STRONG&gt; include any * or similar wildcard character as the index function would require the presence of the * to be a match.&lt;/P&gt;
&lt;P&gt;No claims for particular efficiency but it should be easy to understand.&lt;/P&gt;
&lt;P&gt;SUM is used to increment the counter so we don't have to worry about initializing the count array variables to 0. With this approach you need one variable in the c array for each word to count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need a count across all of the tweets then the easiest would be verify the example code is counting per record correctly and then use a procedure like Proc Means or Summary with the Sum statistic if you need data set of the counts, or Proc Tabulate or Report to create human readable report.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2020 03:44:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dictionary-analysis-of-a-dataset-of-tweets/m-p/688822#M24631</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-05T03:44:09Z</dc:date>
    </item>
  </channel>
</rss>

