<?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 Re: importing data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280144#M56563</link>
    <description />
    <pubDate>Sat, 25 Jun 2016 15:13:42 GMT</pubDate>
    <dc:creator>subrat1</dc:creator>
    <dc:date>2016-06-25T15:13:42Z</dc:date>
    <item>
      <title>importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280061#M56536</link>
      <description>&lt;P&gt;I am trying to import this data set into SAS that has a bunch of variables, and one of those variables is called "MACofResponse". For each observation, there ae multiple MACofResponses, with varying numbers for each example (ie. some have 11, others have 9, and so forth). How would you suggest I go about reading in those variables? My teacher recommended using an array, however I'm not sure how to use an array to read in variables, I only know how to use arrays on variables that are already read in.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2016 20:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280061#M56536</guid>
      <dc:creator>ejohnson96</dc:creator>
      <dc:date>2016-06-24T20:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280072#M56538</link>
      <description>&lt;P&gt;Personally, I'd read it in as one variable - text and then parse it out afterwards in a separate data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are a lot of examples on here on how to do that using COUNTW and SCAN, assuming you have a clear delimiter.&lt;/P&gt;
&lt;P&gt;You do ideally need to know how many you have, unless you want to change your &amp;nbsp;data structure so that it's long - ie one row for every item in the field.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2016 20:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280072#M56538</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-24T20:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280144#M56563</link>
      <description />
      <pubDate>Sat, 25 Jun 2016 15:13:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280144#M56563</guid>
      <dc:creator>subrat1</dc:creator>
      <dc:date>2016-06-25T15:13:42Z</dc:date>
    </item>
    <item>
      <title>Re: importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280150#M56564</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Here's an example that does not require parsing from a character variable. But whether you use Reeza's suggestion or this suggestion, it really depends on the way the input data looks. Is it delimited? Is it a fixed column format? Are you reading from Excel? The structure of the input data will impact the method you will need to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; For example, suppose I have this purchase data in a file called c:\temp\purchases.csv:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;anna,1001,100,200,300
bob,2002,250,75,300,100,150
carla,3003,100
dave,4004,.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To read this data, you should know an "upper" number of purchases that someone might have. In this instance, I know that the max number of purchases is 5. So this is the program I write:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data purchases;
  keep name idnum purch1-purch5 numpurch;
  infile 'c:\temp\purchases.csv' dlm=',' dsd missover;
  input name $ idnum $ purch1-purch5;
  array p_arr purch1-purch5;
  numpurch=0;
  do i = 1 to dim(p_arr);
     if p_arr(i) gt 0 then numpurch=numpurch + 1;
     if p_arr(i) = . then do;
        p_arr(i) = 0;
     end;
  end;    
run;
  
proc print data=purchases;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; With the MISSOVER option, when SAS gets to the end of one line of data, if there are not enough values on the input data line to fill all the "PURCH" variables, then the remaining variables will be set to missing (or .).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Then, if I want those missing values to be set to 0, after I read them in, I declare an ARRAY that is 5 members big and use a DO loop to go through the array and do 2 things: 1) increment a counter called NUMPURCH so I know how many purchases out of the 5 that someone actually had and 2) if a PURCH value is missing assign it a value of 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And, here's the output:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/3747iB787A34F66205457/image-size/original?v=v2&amp;amp;px=-1" alt="purch.png" title="purch.png" border="0" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I don't have any idea of the upper limit, I can pick a very large limit (like 10 or 25 or 50). In this example, I changed the INPUT statement to try to read 10 values for the PURCH variable and so PURCH1-PURCH10 got created, but of course, most of the values were 0 after the DO loop. But I still have the NUMPURCH variable to tell me how many purchases each person had:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/3748i880D2E1877DFBD35/image-size/original?v=v2&amp;amp;px=-1" alt="purch10.png" title="purch10.png" border="0" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and there weren't that many changes in the previous program to accomplish the change -- except for these 3 statements, the rest of the program and the data for the program was the same:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;data purchases_alt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; keep name idnum purch1-&lt;FONT color="#008000"&gt;purch10&lt;/FONT&gt; numpurch;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; infile 'c:\temp\purchases.csv' dlm=',' dsd missover;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; input name $ idnum $ purch1-&lt;FONT color="#008000"&gt;purch10&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array p_arr purch1-&lt;FONT color="#008000"&gt;purch10&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;* * * rest of code same * * *;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or, depending on what your data looks like then you might have to read the data as a character string and then parse out the separate variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2016 15:52:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-data/m-p/280150#M56564</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-06-25T15:52:11Z</dc:date>
    </item>
  </channel>
</rss>

