<?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: Need help creating new variables based on multiple conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-creating-new-variables-based-on-multiple-conditions/m-p/836512#M330747</link>
    <description>&lt;P&gt;To combine all of the datasets horizontally, you need to use a MERGE statement instead of a SET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*now merge all together;
data allyears;
   merge year0 year1 year2;
   BY newid;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Oct 2022 14:01:25 GMT</pubDate>
    <dc:creator>SASJedi</dc:creator>
    <dc:date>2022-10-03T14:01:25Z</dc:date>
    <item>
      <title>Need help creating new variables based on multiple conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-creating-new-variables-based-on-multiple-conditions/m-p/835348#M330258</link>
      <description>&lt;P&gt;I have longitudinal data, with multiple obs per id so I provided only a sample of 4 id's.&amp;nbsp; I have data for many years and i only need to use year0, year 1 and year 2 so i had to create subsets. I have a variable hard_drugs(0/1) that I need to create a new variable for hard drugs that is either never, past or current ( which can be coded as 0,1,2) so if year 0, year 1 and year 2 all have hard_drug 0, then the new variable is never(0). If year 0 and/or year 1 is hard_drug 1 but year 2 is hard_drug 0, then new var is past(1). if year 2 is hard_drug is 1 then that is current(2).&amp;nbsp; I can't figure out how to do that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I need to create a new variable that is the difference between a year2 and year 0 difference. when I try to do this, a new variable is created but the are&amp;nbsp; no values assigned and Im not sure why?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my data:&lt;/P&gt;&lt;P&gt;DATA have;&lt;BR /&gt;INFILE Datalines dlm ="," dsd truncover;&lt;BR /&gt;INPUT newid AGG_MENT AGG_PHYS HASHV HASHF income BMI HBP&lt;BR /&gt;DIAB LIV34 KID FRP FP TCHOL TRIG LDL DYSLIP CESD SMOKE&lt;BR /&gt;DKGRP HEROPIATE IDU LEU3N VLOAD ADH RACE EDUCBA hivpos age&lt;BR /&gt;ART everART years hard_drugs;&lt;/P&gt;&lt;P&gt;DATALINES;&lt;BR /&gt;1,44.90709633,52.52556921,1,.,4,24.71755909,1,1,1,1,1,1,133,176,62,2,14,3,0,1,2,104.1659454,102013,.,1,4,1,52,0,0,0,1&lt;BR /&gt;1,58.20753602,41.29346502,1,4,4,26.06800717,1,1,1,1,1,1,131,107,66,1,2,3,3,1,1,262.0060502,27,2,1,4,1,53,1,1,1,0&lt;BR /&gt;1,59.65135974,48.54453213,1,0,4,27.16420923,1,1,1,1,1,1,180,233,86,2,1,3,0,1,1,345.4009918,60,1,1,4,1,54,1,1,2,0&lt;BR /&gt;1,56.806574,46.73991355,1,0,5,25.71785653,1,1,1,1,1,1,171,139,96,1,18,3,1,1,1,292.3271358,9,1,1,4,1,55,1,1,3,0&lt;BR /&gt;2,46.34189863,27.92331491,1,0,2,26.66936015,2,9,1,9,2,9,125,.,.,2,20,3,0,1,2,257.8277832,8121,.,3,2,1,54,0,0,0,1&lt;BR /&gt;2,48.71790642,38.03806704,2,0,1,25.96576195,2,9,1,9,1,9,.,.,.,9,18,3,3,1,1,459.4561691,21,1,3,2,1,55,1,1,1,0&lt;BR /&gt;2,45.41482533,37.32203958,1,0,2,26.96036793,2,9,1,9,1,1,134,.,.,4,18,3,2,1,1,263.0692533,48,1,3,2,1,56,1,1,2,0&lt;BR /&gt;2,48.05706234,31.80529414,2,0,1,27.56218329,2,1,1,2,1,1,105,104,44,1,21,3,1,1,1,238.6691021,20,1,3,2,1,60,1,1,6,0&lt;BR /&gt;3,40.22336832,60.06969568,1,4,6,28.59084821,9,9,1,1,1,1,170,.,.,2,18,2,0,1,2,563.1222984,4001.556158,,1,7,1,47,0,0,0,1&lt;BR /&gt;3,44.42011462,62.71705483,1,4,6,28.35319907,9,9,1,1,1,1,170,.,.,2,22,2,0,1,2,488.9100325,2020,1,1,7,1,48,1,1,1,1&lt;BR /&gt;3,41.70078566,58.51450134,2,4,6,28.18509751,1,1,1,1,1,1,180,82,127,2,23,2,0,1,2,405.1815688,27.50917363,1,1,7,1,49,1,1,2,1&lt;BR /&gt;3,50.03522655,51.93552598,1,0,.,26.76923019,1,9,1,2,1,1,180,.,.,9,14,2,1,1,2,366.3389365,4.219372675,2,1,7,1,51,1,1,4,1&lt;BR /&gt;3,55.93560718,56.14366763,2,0,7,30.4238763,1,9,2,1,1,1,162,.,.,2,1,2,2,1,2,416.9267951,6.452357495,1,1,7,1,52,1,1,5,1&lt;BR /&gt;4,42.9063758,50.7885018,2,4,1,20.36451325,1,1,1,9,1,1,214,97,147,2,14,3,1,2,1,110.4217933,740,.,1,5,1,44,0,0,0,1&lt;BR /&gt;4,31.15971215,44.62882869,1,2,1,18.21865476,1,3,1,9,2,2,197,.,.,4,25,3,2,2,1,159.6297053,26.64731734,3,1,5,1,45,1,1,1,1&lt;BR /&gt;4,52.68222906,51.50532947,1,0,.,20.28485484,1,1,1,9,1,1,251,260,152,2,13,3,0,1,1,179.6409126,27,2,1,5,1,46,1,1,2,0&lt;BR /&gt;4,50.79010982,57.02273438,.,0,3,21.1071036,1,3,1,1,1,1,234,.,.,4,9,3,1,1,1,313.4508076,29,2,1,5,1,47,1,1,3,0&lt;BR /&gt;4,56.31312223,32.41547791,1,0,3,18.93678664,1,1,1,1,1,1,172,155,97,2,8,3,3,1,1,367.6588994,31,2,1,5,1,48,1,1,4,0&lt;BR /&gt;4,44.07339059,52.80676132,1,0,3,21.60013629,1,1,1,1,1,1,251,129,173,2,6,3,1,1,1,388.0592199,35,2,1,5,1,49,1,1,5,0&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;BR /&gt;*Year 0;&lt;BR /&gt;DATA year0;&lt;BR /&gt;set have (DROP = ART everART HEROPIATE IDU hivpos ADH);&lt;BR /&gt;IF years = 0;&lt;BR /&gt;*Rename outcome variables*;&lt;BR /&gt;RENAME AGG_MENT = agg_ment0&lt;BR /&gt;AGG_PHYS = agg_phys0&lt;BR /&gt;LEU3N = leu3n0&lt;BR /&gt;VLOAD = vload0;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;TITLE 'Year 0 data';&lt;BR /&gt;PROC PRINT DATA = year0;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;*Year 1 only need hard drugs variable;&lt;BR /&gt;DATA year1;&lt;BR /&gt;set have (DROP = AGG_MENT AGG_PHYS HASHV HASHF income BMI HBP&lt;BR /&gt;DIAB LIV34 KID FRP FP TCHOL TRIG LDL DYSLIP CESD SMOKE&lt;BR /&gt;DKGRP HEROPIATE IDU LEU3N VLOAD ADH RACE EDUCBA hivpos age&lt;BR /&gt;ART everART);&lt;BR /&gt;IF years = 1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;TITLE 'Year 1 data';&lt;BR /&gt;PROC PRINT DATA = year1;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;*Year 2;&lt;BR /&gt;DATA year2;&lt;BR /&gt;set have (DROP = ART everART HEROPIATE IDU hivpos);&lt;BR /&gt;IF years = 2;&lt;/P&gt;&lt;P&gt;*Rename outcome variables*;&lt;BR /&gt;RENAME AGG_MENT = agg_ment2&lt;BR /&gt;AGG_PHYS = agg_phys2&lt;BR /&gt;LEU3N = leu3n2&lt;BR /&gt;VLOAD = vload2;&lt;BR /&gt;RUN;&lt;BR /&gt;TITLE 'Year 2 data';&lt;BR /&gt;PROC PRINT DATA = year2;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;*now merge all together;&lt;BR /&gt;data allyears;&lt;BR /&gt;SET year0&lt;BR /&gt;year1&lt;BR /&gt;year2;&lt;BR /&gt;BY newid;&lt;BR /&gt;RUN;&lt;BR /&gt;proc print data =allyears;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA allyears_clean;&lt;BR /&gt;SET allyears;&lt;BR /&gt;agg_ment_diff = agg_ment2 - agg_ment0;&lt;/P&gt;&lt;P&gt;agg_phys_diff =agg_phys2 - agg_phys0;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Sep 2022 00:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-creating-new-variables-based-on-multiple-conditions/m-p/835348#M330258</guid>
      <dc:creator>Zaichik17</dc:creator>
      <dc:date>2022-09-27T00:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need help creating new variables based on multiple conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-creating-new-variables-based-on-multiple-conditions/m-p/836512#M330747</link>
      <description>&lt;P&gt;To combine all of the datasets horizontally, you need to use a MERGE statement instead of a SET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*now merge all together;
data allyears;
   merge year0 year1 year2;
   BY newid;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Oct 2022 14:01:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-creating-new-variables-based-on-multiple-conditions/m-p/836512#M330747</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2022-10-03T14:01:25Z</dc:date>
    </item>
  </channel>
</rss>

