<?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: Hierarchical data file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695731#M212348</link>
    <description>Kurt,&lt;BR /&gt;You gonna missing obs "X123A567F9,A" .</description>
    <pubDate>Sun, 01 Nov 2020 09:12:05 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-11-01T09:12:05Z</dc:date>
    <item>
      <title>Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695718#M212339</link>
      <description>&lt;P&gt;I got a hierarchical data file:&lt;/P&gt;&lt;P class="p1"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pasted Graphic.png" style="width: 714px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51289iCAAAC03597F6B424/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pasted Graphic.png" alt="Pasted Graphic.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;I want to generate one observation per detail record and my code is:&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;data Household;&lt;BR /&gt;infile datalines dlm=',';&lt;BR /&gt;input Household_ID : $15. Type$;&lt;BR /&gt;input DOB : date11. Indicator$ Gender$ Marital_Status$ Education Employment_Status$ Income$;&lt;BR /&gt;Second_field=scan(_infile_,2,',');&lt;BR /&gt;Do while (Second_field ='Y'or'N');&lt;BR /&gt;output;&lt;BR /&gt;input DOB--Income;&lt;BR /&gt;end;&lt;BR /&gt;datalines;&lt;BR /&gt;A1234567BC012,A&lt;BR /&gt;15/FEB/1980,Y,Male,Married,3,FT,55000&lt;BR /&gt;3/JUN/1982,N,Female,Married,3,UE,0&lt;BR /&gt;24/JAN/2005,N,Male,Unknown,2,NA,0&lt;BR /&gt;D135EG0234678,B&lt;BR /&gt;19/OCT/1950,Y,Female,Divorced,0,PT,5000&lt;BR /&gt;X123A567F9,A&lt;BR /&gt;B2345234CC,A&lt;BR /&gt;21/May/1975,N,Male,Married,2,FT,30000&lt;BR /&gt;30/JUN/1978,Y,Female,Married,1,PT,10000&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;But my output is like this:&lt;/P&gt;&lt;P class="p1"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pasted Graphic 2.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51290iFB7C4B66A3EFA965/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pasted Graphic 2.png" alt="Pasted Graphic 2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P class="p1"&gt;How could I generate one observation per detail record?&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 05:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695718#M212339</guid>
      <dc:creator>Lynn23</dc:creator>
      <dc:date>2020-11-01T05:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695722#M212341</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/354270"&gt;@Lynn23&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I got a hierarchical data file:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pasted Graphic.png" style="width: 714px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51289iCAAAC03597F6B424/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pasted Graphic.png" alt="Pasted Graphic.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;I want to generate one observation per detail record and my code is:&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;data Household;&lt;BR /&gt;infile datalines dlm=',';&lt;BR /&gt;input Household_ID : $15. Type$;&lt;BR /&gt;input DOB : date11. Indicator$ Gender$ Marital_Status$ Education Employment_Status$ Income$;&lt;BR /&gt;Second_field=scan(_infile_,2,',');&lt;BR /&gt;Do while (Second_field ='Y'or'N');&lt;BR /&gt;output;&lt;BR /&gt;input DOB--Income;&lt;BR /&gt;end;&lt;BR /&gt;datalines;&lt;BR /&gt;A1234567BC012,A&lt;BR /&gt;15/FEB/1980,Y,Male,Married,3,FT,55000&lt;BR /&gt;3/JUN/1982,N,Female,Married,3,UE,0&lt;BR /&gt;24/JAN/2005,N,Male,Unknown,2,NA,0&lt;BR /&gt;D135EG0234678,B&lt;BR /&gt;19/OCT/1950,Y,Female,Divorced,0,PT,5000&lt;BR /&gt;X123A567F9,A&lt;BR /&gt;B2345234CC,A&lt;BR /&gt;21/May/1975,N,Male,Married,2,FT,30000&lt;BR /&gt;30/JUN/1978,Y,Female,Married,1,PT,10000&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;But my output is like this:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pasted Graphic 2.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51290iFB7C4B66A3EFA965/image-size/large?v=v2&amp;amp;px=999" role="button" title="Pasted Graphic 2.png" alt="Pasted Graphic 2.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P class="p1"&gt;How could I generate one observation per detail record?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Before even attempting to place "one observation per detail record" you should describe what you meant. As in what you expect the output to look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you want something like this to read the data so that you have the household and "type" attached to each of the other lines of data.&lt;/P&gt;
&lt;PRE&gt;data Household;
   infile datalines dlm=',';
   informat Household_ID $15. Type $1.
      DOB date11. indicator $1. Gender $6.
      Marital_Status $15. Education best.
      Employment_Status $5. Income best.

   ;
   retain Household_ID Type houseflag;
   input @;
   if anyalpha(_infile_) = 1 AND houseflag then do;
      output;
      input @1 Household_ID  Type;
      houseflag=1;
   end;
   Else if anyalpha(_infile_) = 1 AND Not(houseflag) then do;
      input @1 Household_ID  Type;
      houseflag=1;
   end;
   else do;
   input DOB  Indicator Gender Marital_Status Education 
        Employment_Status Income;
        houseflag=0;
      output;
   end;
   drop houseflag;
   format DOB date9.;
datalines;
A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG0234678,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/May/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000
;&lt;/PRE&gt;
&lt;P&gt;You really have to watch some timing issues. First you need to identify when the record you are reading is a household. Second you have to identify when when the household is following a household with no data so that the record with no data can be written. Third, you have to make sure the household information is attached to each record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The last bit is accomplished by Retaining the two household values so they are available.&lt;/P&gt;
&lt;P&gt;The first two are accomplished by setting and retaining a flag for set when the current line is a household record. Notice the difference in timing of the OUTPUT statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The input @; loads the current line of data (limit 32K characters) and holds the input at that line. So the values can be tested for conditions. I am using a rule of "if the first character of the record is a letter then this is a household identifier line". If you have any stupid data with a DOB value of Jan 1980, or Unknown, &lt;STRONG&gt;this will not work&lt;/STRONG&gt; and you will have to be very explicit on creating and describing a rule that will separate &lt;STRONG&gt;all&lt;/STRONG&gt; your DOB values from a household identifier. (and need to make sure you have an informat that will read those properly)&lt;/P&gt;
&lt;P&gt;If the current value is a household and the line immediately prior was also, then the OUTPUT is performed before actually inputing the household information so the household appears in the dataset. Other wise only the detail records when read get output, which because of the Retain will have the proper household information, barring bad DOB values...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I moved the informat information to an Informat statement because delimited data and infromats on an input statement often have some issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And for @#$# why read "income" as character????&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 08:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695722#M212341</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-01T08:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695723#M212342</link>
      <description>&lt;P&gt;Use retained variables, and start with a "blind input":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data household;
infile datalines dlm=',';
length
  household $15
  type $1
  dob 4
  indicator $1
  gender $6
  marital_status $8
  education 3
  employment_status $2
  income 8
;
format dob yymmdd10.;
retain household type;
input @;
if countw(_infile_,',') = 2
then input household type;
else do;
  input DOB :date11. Indicator Gender Marital_Status Education Employment_Status Income;
  output;
end;
datalines;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 08:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695723#M212342</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-01T08:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695727#M212345</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
infile datalines length=len;
input temp $varying800. len;
datalines;
A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG0234678,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/May/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000
;
run;
data Household;
 merge temp temp(rename=(temp=_temp) firstobs=2);
if notdigit(temp)=1 then do;
 Household_ID=scan(temp,1,','); 
 Type=scan(temp,2,',');
 retain Household_ID Type ;
 if notdigit(_temp)=1 then output;
end;
else do;
 DOB=scan(temp,1,','); 
 Indicator=scan(temp,2,','); 
 Gender=scan(temp,3,',');
 Marital_Status=scan(temp,4,','); 
 Education=scan(temp,5,',');
 Employment_Status=scan(temp,6,','); 
 Income=scan(temp,7,',');
 output;
end;
drop temp _temp;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 08:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695727#M212345</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-11-01T08:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695731#M212348</link>
      <description>Kurt,&lt;BR /&gt;You gonna missing obs "X123A567F9,A" .</description>
      <pubDate>Sun, 01 Nov 2020 09:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695731#M212348</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-11-01T09:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695732#M212349</link>
      <description>&lt;P&gt;The OP wanted "one observation per detail record", so I inferred that households without details are not wanted. Seems I was right &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 09:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695732#M212349</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-01T09:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: Hierarchical data file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695733#M212350</link>
      <description>Yes you are right. Thank you all!!</description>
      <pubDate>Sun, 01 Nov 2020 09:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hierarchical-data-file/m-p/695733#M212350</guid>
      <dc:creator>Lynn23</dc:creator>
      <dc:date>2020-11-01T09:25:56Z</dc:date>
    </item>
  </channel>
</rss>

