<?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: Logic needed in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59256#M12837</link>
    <description>If your husband &lt;B&gt;ALWAYS&lt;/B&gt; has only one SPOUSE and one CHILD, you can simply use this&lt;BR /&gt;
[pre]&lt;BR /&gt;
data dx; &lt;BR /&gt;
infile datalines ; &lt;BR /&gt;
input NAME :$10. AGE :3. GENDER :$1. RELATION :$10. / S_NAME :$10. / C_NAME :$10.; &lt;BR /&gt;
datalines; &lt;BR /&gt;
A 40 M HUSBAND&lt;BR /&gt;
B 30 F SPOUSE&lt;BR /&gt;
C 10 M CHILD&lt;BR /&gt;
D 40 M HUSBAND&lt;BR /&gt;
E 30 F SPOUSE&lt;BR /&gt;
F 10 M CHILD&lt;BR /&gt;
; &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
But I guess that is not always the case - Below I have added an extra child in the datalines.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data TEST;&lt;BR /&gt;
   length name_HUSBAND type R_Name $10;&lt;BR /&gt;
   retain name_HUSBAND count;&lt;BR /&gt;
   input @8 type :$10. @; put type=;&lt;BR /&gt;
   if type='HUSBAND' then &lt;BR /&gt;
     do;&lt;BR /&gt;
	   count=0;&lt;BR /&gt;
       input @1 name_HUSBAND :$10.;&lt;BR /&gt;
	 end;&lt;BR /&gt;
   else &lt;BR /&gt;
     do; &lt;BR /&gt;
       if type='CHILD' then &lt;BR /&gt;
         do; &lt;BR /&gt;
           count+1; &lt;BR /&gt;
           type=cats(type,count); &lt;BR /&gt;
         end;&lt;BR /&gt;
       input @1 R_Name :$10.;&lt;BR /&gt;
       output TEST;&lt;BR /&gt;
     end;&lt;BR /&gt;
datalines; &lt;BR /&gt;
A 40 M HUSBAND &lt;BR /&gt;
B 30 F SPOUSE &lt;BR /&gt;
C 10 M CHILD &lt;BR /&gt;
D 40 M HUSBAND &lt;BR /&gt;
E 30 F SPOUSE &lt;BR /&gt;
F 10 M CHILD &lt;BR /&gt;
G 12 F CHILD &lt;BR /&gt;
; &lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=test out=trans(drop=_name_) prefix=name_;&lt;BR /&gt;
  by name_HUSBAND;&lt;BR /&gt;
  id type;&lt;BR /&gt;
  var r_name;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Mon, 10 Nov 2008 14:07:17 GMT</pubDate>
    <dc:creator>GertNissen</dc:creator>
    <dc:date>2008-11-10T14:07:17Z</dc:date>
    <item>
      <title>Logic needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59254#M12835</link>
      <description>Kindly help me with the logic for this one. &lt;BR /&gt;
&lt;BR /&gt;
If the relation is husband, then all the other observations following it until another husband occurs should be read into a single record, and i need to get corrresponding spouse and child name in the same husbands record but with spouse and child name.&lt;BR /&gt;
&lt;BR /&gt;
Data to be processed:&lt;BR /&gt;
NAME AGE GENDER RELATION  &lt;BR /&gt;
A        40      M              HUSBAND&lt;BR /&gt;
B       30       F              sPOUSE&lt;BR /&gt;
C       10      M              CHILD &lt;BR /&gt;
D       40      M	   HUSBAND&lt;BR /&gt;
E      30       F	    SPOUSE&lt;BR /&gt;
F      10       M	   CHILD&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
REPORT:&lt;BR /&gt;
&lt;BR /&gt;
Name age gender relation       spouse  child&lt;BR /&gt;
a       40    m        husband	 b      c&lt;BR /&gt;
d      40     m        husband          e      f</description>
      <pubDate>Mon, 03 Nov 2008 02:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59254#M12835</guid>
      <dc:creator>krishnaonline</dc:creator>
      <dc:date>2008-11-03T02:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Logic needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59255#M12836</link>
      <description>Hey Krishna,&lt;BR /&gt;
Try this out &lt;BR /&gt;
&lt;BR /&gt;
data dx;                                                  &lt;BR /&gt;
infile datalines missover;                                &lt;BR /&gt;
input NAME :$10. AGE  :3. GENDER :$1. RELATION :$10.;     &lt;BR /&gt;
datalines;                                                &lt;BR /&gt;
A 40 M HUSBAND                                            &lt;BR /&gt;
B 30 F sPOUSE                                             &lt;BR /&gt;
C 10 M CHILD                                              &lt;BR /&gt;
D 40 M HUSBAND                                            &lt;BR /&gt;
E 30 F SPOUSE                                             &lt;BR /&gt;
F 10 M CHILD                                              &lt;BR /&gt;
;                                                         &lt;BR /&gt;
data dxx;                                                 &lt;BR /&gt;
set dx;                                                   &lt;BR /&gt;
retain counter 0;                                         &lt;BR /&gt;
  if strip(relation) = 'HUSBAND' then counter=counter+1 ; &lt;BR /&gt;
run;                                                      &lt;BR /&gt;
&lt;BR /&gt;
proc summary nway missing data= dxx;                                    &lt;BR /&gt;
class counter ;                                                         &lt;BR /&gt;
output out=sushil(drop=_: relation_2 relation_3 gender_2 gender_3 age_2 age_3  counter)  &lt;BR /&gt;
idgroup( out{3} (NAME AGE GENDER RELATION)=);                           &lt;BR /&gt;
run;                                                                    &lt;BR /&gt;
proc print data=sushil;run;&lt;BR /&gt;
&lt;BR /&gt;
if you don't like proc summary, you can do datastep manipulation after the dxx dataset creation. I kinda love summary procedure, hence used it over here.&lt;BR /&gt;
&lt;BR /&gt;
hope that helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 03 Nov 2008 08:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59255#M12836</guid>
      <dc:creator>SushilNayak</dc:creator>
      <dc:date>2008-11-03T08:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Logic needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59256#M12837</link>
      <description>If your husband &lt;B&gt;ALWAYS&lt;/B&gt; has only one SPOUSE and one CHILD, you can simply use this&lt;BR /&gt;
[pre]&lt;BR /&gt;
data dx; &lt;BR /&gt;
infile datalines ; &lt;BR /&gt;
input NAME :$10. AGE :3. GENDER :$1. RELATION :$10. / S_NAME :$10. / C_NAME :$10.; &lt;BR /&gt;
datalines; &lt;BR /&gt;
A 40 M HUSBAND&lt;BR /&gt;
B 30 F SPOUSE&lt;BR /&gt;
C 10 M CHILD&lt;BR /&gt;
D 40 M HUSBAND&lt;BR /&gt;
E 30 F SPOUSE&lt;BR /&gt;
F 10 M CHILD&lt;BR /&gt;
; &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
But I guess that is not always the case - Below I have added an extra child in the datalines.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data TEST;&lt;BR /&gt;
   length name_HUSBAND type R_Name $10;&lt;BR /&gt;
   retain name_HUSBAND count;&lt;BR /&gt;
   input @8 type :$10. @; put type=;&lt;BR /&gt;
   if type='HUSBAND' then &lt;BR /&gt;
     do;&lt;BR /&gt;
	   count=0;&lt;BR /&gt;
       input @1 name_HUSBAND :$10.;&lt;BR /&gt;
	 end;&lt;BR /&gt;
   else &lt;BR /&gt;
     do; &lt;BR /&gt;
       if type='CHILD' then &lt;BR /&gt;
         do; &lt;BR /&gt;
           count+1; &lt;BR /&gt;
           type=cats(type,count); &lt;BR /&gt;
         end;&lt;BR /&gt;
       input @1 R_Name :$10.;&lt;BR /&gt;
       output TEST;&lt;BR /&gt;
     end;&lt;BR /&gt;
datalines; &lt;BR /&gt;
A 40 M HUSBAND &lt;BR /&gt;
B 30 F SPOUSE &lt;BR /&gt;
C 10 M CHILD &lt;BR /&gt;
D 40 M HUSBAND &lt;BR /&gt;
E 30 F SPOUSE &lt;BR /&gt;
F 10 M CHILD &lt;BR /&gt;
G 12 F CHILD &lt;BR /&gt;
; &lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=test out=trans(drop=_name_) prefix=name_;&lt;BR /&gt;
  by name_HUSBAND;&lt;BR /&gt;
  id type;&lt;BR /&gt;
  var r_name;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 10 Nov 2008 14:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59256#M12837</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2008-11-10T14:07:17Z</dc:date>
    </item>
    <item>
      <title>Re: Logic needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59257#M12838</link>
      <description>Hi:&lt;BR /&gt;
  With just a few modifications to this program, you do NOT need the PROC TRANSPOSE step. You can create one obs for every family by using ARRAYS to hold the childrens' names, ages and genders and by RETAINING all the family information until you've collected 1 family.&lt;BR /&gt;
&lt;BR /&gt;
  Support you had this data stored in c:\temp\famdata.txt:&lt;BR /&gt;
[pre]&lt;BR /&gt;
A 40 M HUSBAND &lt;BR /&gt;
B 30 F SPOUSE &lt;BR /&gt;
C 10 M CHILD &lt;BR /&gt;
D 40 M HUSBAND &lt;BR /&gt;
E 30 F SPOUSE &lt;BR /&gt;
F 10 M CHILD &lt;BR /&gt;
G 12 F CHILD &lt;BR /&gt;
H 40 M HUSBAND &lt;BR /&gt;
I 30 F SPOUSE &lt;BR /&gt;
J 12 M CHILD &lt;BR /&gt;
K 14 F CHILD &lt;BR /&gt;
L 40 M HUSBAND &lt;BR /&gt;
M 12 M CHILD &lt;BR /&gt;
N 14 F CHILD &lt;BR /&gt;
O 40 M HUSBAND&lt;BR /&gt;
P 38 F SPOUSE&lt;BR /&gt;
[/pre]&lt;BR /&gt;
    &lt;BR /&gt;
(Note how the last family has no children and the second to the last family is a single-parent family with a husband and no spouse.)&lt;BR /&gt;
&lt;BR /&gt;
Now the COUNT variable shows the number of children and could be used in subsequent programs as a DO loop counter. Here is the output from the program shown below. &lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;B&gt;The Output&lt;/B&gt;&lt;BR /&gt;
[pre]&lt;BR /&gt;
test&lt;BR /&gt;
                                                                                 &lt;BR /&gt;
        name_                       name_&lt;BR /&gt;
Obs    HUSBAND    h_age    h_gen    Spouse    s_age    s_gen    cn1    cn2    cn3    cn4    cg1    cg2    cg3    cg4    a1    a2    a3    a4    count&lt;BR /&gt;
&lt;BR /&gt;
 1        A         40       M       B          30       F       C                           M                          10     .     .     .      1&lt;BR /&gt;
 2        D         40       M       E          30       F       F      G                    M      F                   10    12     .     .      2&lt;BR /&gt;
 3        H         40       M       I          30       F       J      K                    M      F                   12    14     .     .      2&lt;BR /&gt;
 4        L         40       M       NONE        .               M      N                    M      F                   12    14     .     .      2&lt;BR /&gt;
 5        O         40       M       P          38       F                                                               .     .     .     .      0&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                           &lt;BR /&gt;
using this program:&lt;BR /&gt;
&lt;B&gt; The ChangedProgram&lt;/B&gt;&lt;BR /&gt;
[pre]&lt;BR /&gt;
options nodate nonumber nocenter;&lt;BR /&gt;
data TEST;&lt;BR /&gt;
   keep   name_HUSBAND h_age h_gen&lt;BR /&gt;
          name_SPOUSE s_age s_gen&lt;BR /&gt;
          cn1 cn2 cn3 cn4 &lt;BR /&gt;
          cg1 cg2 cg3 cg4&lt;BR /&gt;
          a1 a2 a3 a4 count;&lt;BR /&gt;
                        &lt;BR /&gt;
   length name_HUSBAND $10 h_age 8 h_gen $1&lt;BR /&gt;
          name_Spouse $10 s_age 8 s_gen $1&lt;BR /&gt;
          type R_Name $10;&lt;BR /&gt;
   array cn $10 cn1-cn4;&lt;BR /&gt;
   array cg $1 cg1-cg4;&lt;BR /&gt;
   array a a1-a4;&lt;BR /&gt;
   retain name_HUSBAND h_age h_gen&lt;BR /&gt;
          name_SPOUSE s_age s_gen&lt;BR /&gt;
          cn1 cn2 cn3 cn4 cg1 cg2 cg3 cg4&lt;BR /&gt;
          a1 a2 a3 a4 count;&lt;BR /&gt;
   label name_Husband = 'Husband'&lt;BR /&gt;
         name_Spouse = 'Spouse'&lt;BR /&gt;
         count = 'Number Children';&lt;BR /&gt;
                    &lt;BR /&gt;
   ** note: can only use END= with a FILE, not with DATALINES;&lt;BR /&gt;
   infile 'c:\temp\famdata.txt' pad end=eof;&lt;BR /&gt;
   input @8 type :$10. @; &lt;BR /&gt;
   put type=;&lt;BR /&gt;
   if type='HUSBAND' then do;&lt;BR /&gt;
       ** Output "saved" info every time a new HUSBAND dataline;&lt;BR /&gt;
       ** is read -- after first dataline.;&lt;BR /&gt;
       ** (when on 1st dataline, have not read the whole family yet);&lt;BR /&gt;
       ** But when on a HUSBAND record, the previous family has been read.;&lt;BR /&gt;
       if _n_ gt 1 then output TEST;&lt;BR /&gt;
       &lt;BR /&gt;
       ** input new HUSBAND record;&lt;BR /&gt;
       input @1 name_HUSBAND $ h_age h_gen $;&lt;BR /&gt;
                                                          &lt;BR /&gt;
       ** clear arrays for retained children info and ;&lt;BR /&gt;
       ** reset spouse info for new family;&lt;BR /&gt;
       do i = 1 to 4 by 1;&lt;BR /&gt;
         cn(i) = ' ';&lt;BR /&gt;
         cg(i) = ' ';&lt;BR /&gt;
         a(i) = .;&lt;BR /&gt;
       end;&lt;BR /&gt;
       name_SPOUSE='NONE';&lt;BR /&gt;
       s_age = .;&lt;BR /&gt;
       s_gen = ' ';&lt;BR /&gt;
       count = 0;&lt;BR /&gt;
   end;&lt;BR /&gt;
   else do; &lt;BR /&gt;
       ** Read input dataline into 2 holding values and;&lt;BR /&gt;
       ** then assign to variables based on type.;&lt;BR /&gt;
       input @1 R_Name :$10. r_age r_gen $;&lt;BR /&gt;
                 &lt;BR /&gt;
       if type='CHILD' then do; &lt;BR /&gt;
         count+1; &lt;BR /&gt;
         cn(count) = R_Name;&lt;BR /&gt;
         cg(count) = r_gen;&lt;BR /&gt;
         a(count) = R_Age;&lt;BR /&gt;
       end;&lt;BR /&gt;
       if type = 'SPOUSE' then do;&lt;BR /&gt;
         name_SPOUSE = R_Name;&lt;BR /&gt;
         s_age = R_Age;&lt;BR /&gt;
         s_gen = r_gen;&lt;BR /&gt;
       end;&lt;BR /&gt;
   end;&lt;BR /&gt;
          &lt;BR /&gt;
   ** on last dataline, output the last family;&lt;BR /&gt;
   if eof = 1 then output TEST;&lt;BR /&gt;
run;&lt;BR /&gt;
             &lt;BR /&gt;
proc print data=test;&lt;BR /&gt;
title 'test';&lt;BR /&gt;
run;&lt;BR /&gt;
                 &lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 10 Nov 2008 23:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-needed/m-p/59257#M12838</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-11-10T23:38:14Z</dc:date>
    </item>
  </channel>
</rss>

