<?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: Combining Data Vertically in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696685#M212837</link>
    <description>&lt;P&gt;Since you have things that need to be done to the variables depending on the source of the data it might be worth looking at the data set option IN= to create variables that lets you know which data set is contributing the current record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did they provide any code to create the data sets so you can test it? You likely need to consider the lengths of character variables as well. We don't have enough information to help with that.&lt;/P&gt;</description>
    <pubDate>Wed, 04 Nov 2020 20:57:18 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-11-04T20:57:18Z</dc:date>
    <item>
      <title>Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696659#M212822</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;I am working in SAS studio for SAS On Demand trying to figure out a hw question for combining data vertically. Here is the question and what the results are supposed to look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble getting the firstnm and lastnm variables together and combined with the name variable from the other dataset. Here is my code that I have written so far:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sites;&lt;BR /&gt;set CM (rename = (Gender = SexCd1&lt;BR /&gt;))&lt;BR /&gt;hospital (rename = (Site = Institution&lt;BR /&gt;PatientID = SubjID&lt;BR /&gt;Sex = SexCd1&lt;BR /&gt;));&lt;BR /&gt;if SexCd1 = 'Male' then SexCd = 'M';&lt;BR /&gt;else if SexCd1 = 'Female' then SexCd = 'F';&lt;BR /&gt;SexCd = propcase(SexCd1);&lt;BR /&gt;Name1 = catx(', ',LastNm,FirstNm);&lt;BR /&gt;drop SexCd1 FirstNm LastNm;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know what I can do. Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sincerely,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kaitlin E Buck&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2020 15:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696659#M212822</guid>
      <dc:creator>kateb409</dc:creator>
      <dc:date>2020-12-10T15:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696667#M212826</link>
      <description>Do you have to do this in one step or will multiple steps be valid?&lt;BR /&gt;&lt;BR /&gt;I would personally start by listing all the steps you need to do - ie recode Male/Female/First name and then check them off.</description>
      <pubDate>Wed, 04 Nov 2020 20:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696667#M212826</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-11-04T20:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696685#M212837</link>
      <description>&lt;P&gt;Since you have things that need to be done to the variables depending on the source of the data it might be worth looking at the data set option IN= to create variables that lets you know which data set is contributing the current record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did they provide any code to create the data sets so you can test it? You likely need to consider the lengths of character variables as well. We don't have enough information to help with that.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2020 20:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696685#M212837</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-04T20:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696723#M212859</link>
      <description>&lt;P&gt;There was a similar topic recently.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/td-p/696072" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/td-p/696072&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have1;
	infile datalines dlm="09"x;
	input Site:$11. PatientID FirstNm $ LastNm $ Sex $;
	datalines;
St Johns	203	Daniel	Taylor	M
St Johns	206	Helen	Davis	F
St Johns	208	Betty	Smith	f
;
run;

data have2;
	infile datalines dlm="09"x;
	input Institution:$11. SubjID Name:$15. Gender $;
	datalines;
City Medics	102	Wilson, Steven	Male
City Medics	105	Moore, Chris	male
City Medics	109	Jackson, Sharon	Female
;
run;
DATA want;
  if _N_=1 then
    do;
      RETAIN Institution;
      LENGTH Institution $11. SubjID 8 Name $15. sexcd $10. ;

      declare hash h(multidata:"y",ordered:"y");
      h.definekey("Institution");
      h.definedata("Institution","SubjID","name","sexcd");
      h.definedone();
      call missing(subjid);
      declare hiter hi("h");
    end;

do until(last1);
  SET WORK.have1 (RENAME=(Site=Institution PatientID=SubjID sex=sexcd)) end=last1;
  Name=FirstNm||","||LastNm;
  sexcd=upcase(sexcd);
  h.add();
end;

do until(last2);
  set have2(RENAME=(Gender=SexCd)) end=last2;
  if upcase(sexcd)="MALE" then sexcd="M";
  else sexcd="F";
  h.add();
end;

do while(hi.next()=0);
  output;
end;

drop firstnm lastnm;

RUN;
*proc sql;
proc sql;
create table want as
  select a.institution,
         a.subjid,
         cats(a.firstnm,",",a.lastnm) as name,
         upcase(a.sexcd)
    from WORK.have1 (RENAME=(Site=Institution PatientID=SubjID sex=sexcd)) a
    union 
  select b.institution,
         b.subjid,
         b.name,
         case upcase(b.gender) when "MALE" then "M" else "F" end as sexcd 
    from work.have2 b
      order by 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2020 23:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696723#M212859</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-04T23:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696735#M212863</link>
      <description>&lt;P&gt;First list what you need to do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/*List of things to do:
Table 1: 
Rename: Site to Institution, PatientID to SubjID, Sex to SexCd
Upcase: SexCD (make capital letters)
Modify: Combine lastnm and firstnm variables

Table 2:
Rename:Gender to SexCD
Upcase:SexCD - take first character and upcase

Order variables via RETAIN
Stack tables via SET statements
*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would then proceed to doing each one of these and breaking them down further if required. The answer is below if you need further help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;P&gt;&lt;BR /&gt;data have3;&lt;BR /&gt;retain Institution SubjID Name Gender;&lt;BR /&gt;set have1 (in=T1 rename = (Sex = SexCd &lt;BR /&gt;PatientID = SubjID &lt;BR /&gt;Site = Institution))&lt;BR /&gt;have2 (in=T2);&lt;BR /&gt;&lt;BR /&gt;if T1 then Name = catx(", ", lastNm, FirstNm);&lt;/P&gt;
&lt;P&gt;if T2 then SexCd = char(Gender, 1);&lt;/P&gt;
&lt;P&gt;SexCd = upcase(SexCd);&lt;/P&gt;
&lt;P&gt;keep Institution SubjID Name SexCd;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc print data=have3;&lt;BR /&gt;run;&lt;/P&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Wed, 04 Nov 2020 23:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696735#M212863</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-11-04T23:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696743#M212866</link>
      <description>&lt;P&gt;Hi, In looking at your posted code, it doesn't quite make sense to me. Here's some highlighting to illustrate my confusion:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1604537443334.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51394i579AB0DA7619CD86/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1604537443334.png" alt="Cynthia_sas_0-1604537443334.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To start,&amp;nbsp; &lt;STRONG&gt;SexCd1&lt;/STRONG&gt; is renamed from the &lt;STRONG&gt;Gender&lt;/STRONG&gt; variable in WORK.CM and from the &lt;STRONG&gt;Sex&lt;/STRONG&gt; variable in WORK.STJOHNS. Then, you are creating a new variable named &lt;STRONG&gt;SexCd&lt;/STRONG&gt; to be either M or F, based on the value of &lt;STRONG&gt;SexCd1 &lt;/STRONG&gt;. So, after the IF/ELSE IF, you now have &lt;STRONG&gt;SexCd&lt;/STRONG&gt; with values of either M or F based on &lt;STRONG&gt;SexCd1&lt;/STRONG&gt;. Then, in the very next assignment statement you assign the PROPCASE value of &lt;STRONG&gt;SexCd1&lt;/STRONG&gt; over top of the &lt;STRONG&gt;SexCd&lt;/STRONG&gt; value of M or the F that you just assigned. Then you drop SexCd1. I'm not sure why you're doing this. What is the purpose of the IF statement where you test&lt;/P&gt;
&lt;P&gt;if SexCd1 = 'Male' then SexCd = 'M'&lt;/P&gt;
&lt;P&gt;else if SexCd1 = 'Female' then SexCd = 'F';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Also, you don't have a LENGTH for SexCd, so what is your expected length for this new variable? Can you clarify what the logic of your program is doing or supposed to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And, will the &lt;STRONG&gt;SexCd1&lt;/STRONG&gt; value ALWAYS be "Male" or "Female" or is it possible for the values to be "male" and "female"? In which case, you have a different problem with the IF statement that creates the &lt;STRONG&gt;SexCd&lt;/STRONG&gt; variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I would be interested in seeing a PROC FREQ of Gender from WORK.CM and a PROC FREQ of Sex from WORK.STJOHNS to verify what the starting values are for each of those original variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just some thoughts because without data, no one can run your code.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 05 Nov 2020 01:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696743#M212866</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-11-05T01:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data Vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696744#M212867</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;This did work, thank you! I feel like I was trying to do too many things and was not sure how to order the different statements in the data step. Can you explain to me what the in= operator does again? Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kaitlin E Buck&lt;/P&gt;</description>
      <pubDate>Thu, 05 Nov 2020 01:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Data-Vertically/m-p/696744#M212867</guid>
      <dc:creator>kateb409</dc:creator>
      <dc:date>2020-11-05T01:18:58Z</dc:date>
    </item>
  </channel>
</rss>

