<?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: Reading Raw Hierarchical data files in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5246#M2064</link>
    <description>Thanks Cynthia&lt;BR /&gt;
&lt;BR /&gt;
apologies my data looks like this&lt;BR /&gt;
&lt;BR /&gt;
id 263 bobby&lt;BR /&gt;
a1 5 7 9 10&lt;BR /&gt;
a2 6 3 7 4&lt;BR /&gt;
id 432 colin&lt;BR /&gt;
a1 7 3 10 5&lt;BR /&gt;
a2 6 3 7 4&lt;BR /&gt;
id 956 tracy&lt;BR /&gt;
a1 7 3 10 5&lt;BR /&gt;
a2 7 8 6 7&lt;BR /&gt;
id 765 cindy&lt;BR /&gt;
a1 7 9 5 4&lt;BR /&gt;
a2 3 9 8 4&lt;BR /&gt;
&lt;BR /&gt;
so it's no wonder the program went cahoot.&lt;BR /&gt;
&lt;BR /&gt;
however, I read your help and  yes I didn't "reset" or "reinitialize" the PDV&lt;BR /&gt;
so from this I fixed my code to reflect this and syntax errors of the if else then do cases .What did you know, this baby worked like a charm.&lt;BR /&gt;
&lt;BR /&gt;
Thank you so much Cynthia for your help, that "reset" or "reinitialize" really  helped me&lt;BR /&gt;
out otherwise I would have wasted time trying to figure it out.&lt;BR /&gt;
&lt;BR /&gt;
-S&lt;BR /&gt;
you know, worked like a charm.</description>
    <pubDate>Sat, 27 Oct 2007 20:47:41 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2007-10-27T20:47:41Z</dc:date>
    <item>
      <title>Reading Raw Hierarchical data files</title>
      <link>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5244#M2062</link>
      <description>Ok my problem is I want to read a hierarchical raw data set like this one:&lt;BR /&gt;
&lt;BR /&gt;
bobby 213&lt;BR /&gt;
m1 5 7 9 10&lt;BR /&gt;
colin 442&lt;BR /&gt;
m1 7 3 10 5&lt;BR /&gt;
m2 6 3 7 4&lt;BR /&gt;
tracy 856&lt;BR /&gt;
m2 7 8 6 7&lt;BR /&gt;
cindy 365&lt;BR /&gt;
m1 7 9 4 5&lt;BR /&gt;
m2 3 9 8 4&lt;BR /&gt;
&lt;BR /&gt;
and for SAS to output a result like this:&lt;BR /&gt;
&lt;BR /&gt;
id    name   mark1   mark2   total&lt;BR /&gt;
213   bobby   31      0       31&lt;BR /&gt;
442   colin   25      20      45&lt;BR /&gt;
856   tracy    0      28      28&lt;BR /&gt;
365   cindy   25      24      49&lt;BR /&gt;
&lt;BR /&gt;
mark1 is total of marks in m1&lt;BR /&gt;
mark2 is total of marks in m2&lt;BR /&gt;
total is equal to the sum of mark1 and mark2&lt;BR /&gt;
&lt;BR /&gt;
this is my code so far:&lt;BR /&gt;
&lt;BR /&gt;
proc fslist fileref='marks.txt';&lt;BR /&gt;
run;&lt;BR /&gt;
data marks (drop=Type q1 q2 q3 q4);&lt;BR /&gt;
/* Drop inputs Type question1 = q1 - question4 = q4 */&lt;BR /&gt;
retain id name q1 q2  q3  q4 mark1 mark2;&lt;BR /&gt;
length id $3. name $10. Type $2. q1 3. q2 3. q3 3. q4 3.;&lt;BR /&gt;
/* Keeping the length of all inputs for neatness */&lt;BR /&gt;
&lt;BR /&gt;
infile 'marks.txt' end=LastRec;&lt;BR /&gt;
&lt;BR /&gt;
input Type $@;&lt;BR /&gt;
&lt;BR /&gt;
if Type='id' then do;&lt;BR /&gt;
        if _N_ &amp;gt; 1 then output;&lt;BR /&gt;
        input  id $ name $; end;&lt;BR /&gt;
&lt;BR /&gt;
else do;  &lt;BR /&gt;
  &lt;BR /&gt;
input q1 q2 q3 q4;&lt;BR /&gt;
&lt;BR /&gt;
if (Type = 'm1') and (Type ^= 'm2') and (Type ^= '.') then&lt;BR /&gt;
mark1 = sum(q1,q2,q3,q4);&lt;BR /&gt;
*total = mark1;&lt;BR /&gt;
&lt;BR /&gt;
else if (Type ^= 'm1') and (Type = 'm2') and (Type ^= '.') then&lt;BR /&gt;
mark2 = sum(q1,q2,q3,q4);&lt;BR /&gt;
*total = mark2;&lt;BR /&gt;
&lt;BR /&gt;
/*&lt;BR /&gt;
if (Type = 'm1') and (Type ^= 'm2') then&lt;BR /&gt;
mark1 = sum(q1,q2,q3,q4);&lt;BR /&gt;
*/&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/*&lt;BR /&gt;
else if (Type = 'm2') and (Type ^= 'm1') then&lt;BR /&gt;
mark2 = sum(q1,q2,q3,q4);&lt;BR /&gt;
*/&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/*&lt;BR /&gt;
else if (Type = 'm1') and (Type = 'm2')&lt;BR /&gt;
mark1 = sum(q1,q2,q3,q4);&lt;BR /&gt;
mark2 = sum(q1,q2,q3,q4);&lt;BR /&gt;
*/&lt;BR /&gt;
&lt;BR /&gt;
end;&lt;BR /&gt;
if LastRec then output;&lt;BR /&gt;
run;&lt;BR /&gt;
ods html file='marks.html';&lt;BR /&gt;
proc print data=marks noobs;&lt;BR /&gt;
title 'Marks';&lt;BR /&gt;
run;&lt;BR /&gt;
ods html close;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
so far my output looks like this:&lt;BR /&gt;
&lt;BR /&gt;
id    name   mark1   mark2&lt;BR /&gt;
213   bobby   31      .       &lt;BR /&gt;
442   colin   25      20      &lt;BR /&gt;
856   tracy   25      28      &lt;BR /&gt;
365   cindy   25      24   &lt;BR /&gt;
&lt;BR /&gt;
as you can see id 856 should not have a mark for m1   &lt;BR /&gt;
&lt;BR /&gt;
Sorry for such a long question but I couldn't find any SAS forum on this topic and in manipulating data files like this. Yeah out of ideas on how I should fix this code to&lt;BR /&gt;
reflect the output.&lt;BR /&gt;
&lt;BR /&gt;
Thanks to whoever can help</description>
      <pubDate>Sat, 27 Oct 2007 13:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5244#M2062</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-10-27T13:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reading Raw Hierarchical data files</title>
      <link>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5245#M2063</link>
      <description>Hi:&lt;BR /&gt;
  When I make a copy of your data and run the program, I get this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
NOTE: The infile 'c:\temp\marks.txt' is:&lt;BR /&gt;
      File Name=c:\temp\marks.txt,&lt;BR /&gt;
      RECFM=V,LRECL=256&lt;BR /&gt;
&lt;BR /&gt;
NOTE: Invalid data for q2 in line 2 1-2.&lt;BR /&gt;
RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0&lt;BR /&gt;
2         m1 5 7 9 10 11&lt;BR /&gt;
id=  name=  q1=213 q2=. q3=5 q4=7 mark1=. mark2=. Type=bo LastRec=0 _ERROR_=1 _N_=1&lt;BR /&gt;
NOTE: Invalid data for q2 in line 4 1-2.&lt;BR /&gt;
4         m1 7 3 10 5 11&lt;BR /&gt;
id=  name=  q1=442 q2=. q3=7 q4=3 mark1=. mark2=. Type=co LastRec=0 _ERROR_=1 _N_=2&lt;BR /&gt;
NOTE: Invalid data for q2 in line 7 1-2.&lt;BR /&gt;
7         m2 7 8 6 7 10&lt;BR /&gt;
id=  name=  q1=856 q2=. q3=7 q4=8 mark1=. mark2=20 Type=tr LastRec=0 _ERROR_=1 _N_=4&lt;BR /&gt;
NOTE: Invalid data for q2 in line 9 1-2.&lt;BR /&gt;
9         m1 7 9 4 5 10&lt;BR /&gt;
id=  name=  q1=365 q2=. q3=7 q4=9 mark1=. mark2=20 Type=ci LastRec=0 _ERROR_=1 _N_=5&lt;BR /&gt;
NOTE: 10 records were read from the infile 'c:\temp\marks.txt'.&lt;BR /&gt;
      The minimum record length was 9.&lt;BR /&gt;
      The maximum record length was 11.&lt;BR /&gt;
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.&lt;BR /&gt;
NOTE: The data set WORK.MARKS has 1 observations and 4 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Which is understandable. If your data REALLY does look like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
bobby 213&lt;BR /&gt;
m1 5 7 9 10&lt;BR /&gt;
colin 442&lt;BR /&gt;
m1 7 3 10 5&lt;BR /&gt;
m2 6 3 7 4&lt;BR /&gt;
tracy 856&lt;BR /&gt;
m2 7 8 6 7&lt;BR /&gt;
cindy 365&lt;BR /&gt;
m1 7 9 4 5&lt;BR /&gt;
m2 3 9 8 4&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Then this test NEVER is true: [pre] if Type='id' then do; [/pre]&lt;BR /&gt;
There will NEVER be a value for TYPE = 'id', unless your data really looks like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
id bobby 213&lt;BR /&gt;
m1 5 7 9 10&lt;BR /&gt;
id colin 442&lt;BR /&gt;
m1 7 3 10 5&lt;BR /&gt;
m2 6 3 7 4&lt;BR /&gt;
id tracy 856&lt;BR /&gt;
m2 7 8 6 7&lt;BR /&gt;
id cindy 365&lt;BR /&gt;
m1 7 9 4 5&lt;BR /&gt;
m2 3 9 8 4&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Even then, your INPUT statement is wrong because you have:&lt;BR /&gt;
[pre]&lt;BR /&gt;
input id $ name $;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
BUT in the data you show, NAME is first and ID value is second.&lt;BR /&gt;
 &lt;BR /&gt;
There is a way to read the data, as you show it in your post. However, you have some other issues in your program. It seems that you might also have problems with the DO/END for your IF statement.&lt;BR /&gt;
 &lt;BR /&gt;
The way SAS IF statements work is this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  if Type = 'm1' then mark1 = sum(q1,q2,q3,q4);  &lt;BR /&gt;
 &lt;BR /&gt;
OR&lt;BR /&gt;
  &lt;BR /&gt;
  if LastRec then output;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 are both good without a DO/END because you are only executing ONE statement; however, if you want to execute MORE than ONE statement as a result of a true condition, then you have to do this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    if Type = 'm1' then do; &lt;BR /&gt;
       mark1 = sum(q1,q2,q3,q4); &lt;BR /&gt;
       total + mark1; &lt;BR /&gt;
    end;&lt;BR /&gt;
    else if Type = 'm2' then do; &lt;BR /&gt;
       mark2 = sum(q1,q2,q3,q4); &lt;BR /&gt;
       total + mark2; &lt;BR /&gt;
    end;&lt;BR /&gt;
[/pre] &lt;BR /&gt;
&lt;BR /&gt;
The IF statement needs a DO/END because 2 statements are being executed when Type = 'm1'. The ELSE needs a DO/END because 2 statements are being executed for that condition, as well. I'm one of those people who almost ALWAYS codes a DO/END, anyway... even if I only want to execute one statement. &lt;BR /&gt;
So, if I wasn't going to calculate TOTAL, I'd still do this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    if Type = 'm1' then do; &lt;BR /&gt;
       mark1 = sum(q1,q2,q3,q4); &lt;BR /&gt;
    end;&lt;BR /&gt;
    else if Type = 'm2' then do; &lt;BR /&gt;
       mark2 = sum(q1,q2,q3,q4); &lt;BR /&gt;
    end;&lt;BR /&gt;
  &lt;BR /&gt;
[/pre] &lt;BR /&gt;
Because it helps me see the structure of my conditions and then if I do need to go back and put another action into one or the other of the conditions, I only have to add my statement, I don't have to worry about where the END goes because I already have the logic structure in the program.&lt;BR /&gt;
&lt;BR /&gt;
As for your issue of ID 856 being wrong....that's because you are RETAINING the values for &lt;BR /&gt;
[pre]&lt;BR /&gt;
retain id name q1 q2 q3 q4 mark1 mark2;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
which means that unless you reset them when you do your OUTPUT, what's in the PDV is the information from the PREVIOUS record. Since you retained the information, it is up to your to "reset" or "reinitialize" the PDV:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    if _N_ &amp;gt; 1 then do;&lt;BR /&gt;
       output;&lt;BR /&gt;
      ** when on a name record, after you output the previous record, you;&lt;BR /&gt;
      ** need to "reset" or  "erase" or "reinitialize" retained values in PDV;&lt;BR /&gt;
      ** because these values are left over from previous record (due to retain);&lt;BR /&gt;
      q1 = .;&lt;BR /&gt;
      q2 = .;&lt;BR /&gt;
      q3 = .;&lt;BR /&gt;
      q4 = .;&lt;BR /&gt;
      mark1 = .;&lt;BR /&gt;
      mark2 = .;&lt;BR /&gt;
      total = .;&lt;BR /&gt;
    end;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The reason there is no forum for these kinds of questions is that these are the kinds of questions that are best answered by Tech Support. Your question is very specific to your data -- and in this case, the data you posted doesn't quite match the program you posted. Tech Support has the resources to look at your actual data file and to help you debug how to read the file in the most accurate and efficient manner.&lt;BR /&gt;
&lt;BR /&gt;
To find out how to open a track with Tech Support, go to: &lt;A href="http://support.sas.com" target="_blank"&gt;http://support.sas.com&lt;/A&gt; and in the navigation area on the left, you will see a link that says "Submit a Problem". If you click on that link, you will be able to submit your problem to Tech Support.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Sat, 27 Oct 2007 16:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5245#M2063</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2007-10-27T16:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Reading Raw Hierarchical data files</title>
      <link>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5246#M2064</link>
      <description>Thanks Cynthia&lt;BR /&gt;
&lt;BR /&gt;
apologies my data looks like this&lt;BR /&gt;
&lt;BR /&gt;
id 263 bobby&lt;BR /&gt;
a1 5 7 9 10&lt;BR /&gt;
a2 6 3 7 4&lt;BR /&gt;
id 432 colin&lt;BR /&gt;
a1 7 3 10 5&lt;BR /&gt;
a2 6 3 7 4&lt;BR /&gt;
id 956 tracy&lt;BR /&gt;
a1 7 3 10 5&lt;BR /&gt;
a2 7 8 6 7&lt;BR /&gt;
id 765 cindy&lt;BR /&gt;
a1 7 9 5 4&lt;BR /&gt;
a2 3 9 8 4&lt;BR /&gt;
&lt;BR /&gt;
so it's no wonder the program went cahoot.&lt;BR /&gt;
&lt;BR /&gt;
however, I read your help and  yes I didn't "reset" or "reinitialize" the PDV&lt;BR /&gt;
so from this I fixed my code to reflect this and syntax errors of the if else then do cases .What did you know, this baby worked like a charm.&lt;BR /&gt;
&lt;BR /&gt;
Thank you so much Cynthia for your help, that "reset" or "reinitialize" really  helped me&lt;BR /&gt;
out otherwise I would have wasted time trying to figure it out.&lt;BR /&gt;
&lt;BR /&gt;
-S&lt;BR /&gt;
you know, worked like a charm.</description>
      <pubDate>Sat, 27 Oct 2007 20:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Reading-Raw-Hierarchical-data-files/m-p/5246#M2064</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-10-27T20:47:41Z</dc:date>
    </item>
  </channel>
</rss>

