<?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: Code for New Variables not producing output - Using If-Then statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612435#M178706</link>
    <description>&lt;P&gt;I can only say:&amp;nbsp;&lt;STRONG&gt;Maxim 3&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Know Your Data&lt;/STRONG&gt; includes assigned formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And comparisons always use the raw values, not the formatted ones.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Dec 2019 15:57:12 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-12-17T15:57:12Z</dc:date>
    <item>
      <title>Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611728#M178350</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stumped on what is not working here. My goal is to code the LBCFent variable as positive if either Fental or Norfentanyl are positive (=1). The output for the Proc Freq is good for both Fentnyl and Norfentanyl but the LBCFent is completely empty. Any suggestions?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data LBCFin4;
 set LBCFin3;
 length LBCFent $50.; 
 format LBCFent $50.;
 Fentanyl =strip(Fentanyl);
 Norfentanyl=strip(Norfentanyl);
 Oxycodone = strip(Oxycodone);
 Oxymorphone= strip(Oxymorphone);

if  Fentanyl =1 or Norfentanyl =1 then LBCFent = "Pos"; 
run;


proc freq data=LBCFin4; tables Fentanyl Norfentanyl ; run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 20:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611728#M178350</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-13T20:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611729#M178351</link>
      <description>&lt;P&gt;You don't say whether your variables are character or numeric ... but its reasonable to guess that they are character, in which case you need&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if  Fentanyl ='1' or Norfentanyl ='1' then LBCFent = "Pos"; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Either that or your variables fentanyl and norfentanyl are never equal to one, did you check that?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 21:03:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611729#M178351</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-13T21:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611744#M178360</link>
      <description>&lt;P&gt;Please post some example data, so we have something to test code against. Use a data step with datalines.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 21:51:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611744#M178360</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T21:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611755#M178368</link>
      <description>&lt;P&gt;HI there. Sample variables and code in SAS Window.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Fentanyl                Norfentanyl                    MRN
1                            1                                     123
1                            0                                     234
0                            0                                     345
0                            0                                     456
1                            1                                     567
1                            0                                     678
1                            1                                     789
1                            0                                     890
0                            0                                     910
0                            0                                     321
1                            1                                     432
1                            1                                     543
1                            1                                     654
1                            0                                     765
1                            1                                     876
1                            0                                     987
0                            0                                     990&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 22:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611755#M178368</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-13T22:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611760#M178373</link>
      <description>&lt;P&gt;&lt;STRONG&gt;PLEASE POST EXAMPLE DATA IN A DATA STEP WITH DATALINES.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Otherwise we have no idea about variable types and other attributes like lengths, formats involved, etc.&lt;/P&gt;
&lt;P&gt;Help us to help you.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 23:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611760#M178373</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T23:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611764#M178375</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes the variables are character. Yes I did check the spreadsheet and all variables are coded 0 or 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Susan&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 23:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611764#M178375</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-13T23:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611765#M178376</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data LabConf;
   input Fentanyl $ Norfentanyl $ MRN;
   datalines;
1 0 123
1 1 234
1 1 345
0 0 456
0 1 567
1 1 678
0 1 789
1 0 890
1 1 910
0 0 321
1 0 432
0 1 543
1 1 654
0 1 765
0 0 876
0 0 987
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 23:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611765#M178376</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-13T23:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611766#M178377</link>
      <description>&lt;P&gt;Then you should also use string literals in your condition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if  strip(Fentanyl) = "1" or strip(Norfentanyl) = "1" then LBCFent = "Pos";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or convert everything to numbers:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if  input(Fentanyl,best.) = 1 or input(Norfentanyl,best.) = 1 then LBCFent = "Pos";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that still does not give you the expected results, please post the log of your code.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 23:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611766#M178377</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T23:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611910#M178455</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It did not work. The proc freq output show 1's for Fentanyl and Norfentanyl but the "Pos" is not there for LBCFent. Any thoughts?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LOG:

74   data LBCFin4;
75    set LBCFin3;
76    length LBCFent $50.;
77    format LBCFent $50.;
78    if  strip(Fentanyl) = "1" or strip(Norfentanyl) = "1" then
78 ! LBCFent = 'Pos';
79    run;

NOTE: There were 191 observations read from the data set WORK.LBCFIN3.
NOTE: The data set WORK.LBCFIN4 has 191 observations and 22 variables.
NOTE: Compressing data set WORK.LBCFIN4 increased size by 100.00
      percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds


80   proc freq data=LBCFin4; tables Fentanyl Norfentanyl LBCFent ; run
80 ! ;

NOTE: There were 191 observations read from the data set WORK.LBCFIN4.
NOTE: PROCEDURE FREQ used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Dec 2019 19:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/611910#M178455</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-15T19:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612313#M178642</link>
      <description>&lt;P&gt;It WORKS.&lt;/P&gt;
&lt;P&gt;Proof:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Fentanyl $ Norfentanyl $ MRN;
datalines;
1 0 123
1 1 234
1 1 345
0 0 456
0 1 567
1 1 678
0 1 789
1 0 890
1 1 910
0 0 321
1 0 432
0 1 543
1 1 654
0 1 765
0 0 876
0 0 987
;

data want;
set have;
length LBCFent $50.;
format LBCFent $50.;
if  strip(Fentanyl) = "1" or strip(Norfentanyl) = "1" then LBCFent = 'Pos';
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Fentanyl    Norfentanyl    MRN    LBCFent

   1             0         123    Pos      
   1             1         234    Pos      
   1             1         345    Pos      
   0             0         456             
   0             1         567    Pos      
   1             1         678    Pos      
   0             1         789    Pos      
   1             0         890    Pos      
   1             1         910    Pos      
   0             0         321             
   1             0         432    Pos      
   0             1         543    Pos      
   1             1         654    Pos      
   0             1         765    Pos      
   0             0         876             
   0             0         987             
&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2019 06:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612313#M178642</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-17T06:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612416#M178695</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Good Morning Kurt! &lt;BR /&gt;Thank you so much but my code is still not working. I have attached some sample data and my code up &lt;BR /&gt;and including that point. It makes no sense  to me why it is not working. &lt;BR /&gt;Any insight or suggestions are appreciated! Thanks! Susan&lt;BR /&gt;&lt;BR /&gt;data have;
input MRN $ Fentanyl $ Norfentanyl ;
datalines;&lt;BR /&gt;123	Fentanyl	Positive
123	Norfentanyl 	Positive
123	Oxy		Negative
234	Fentanyl	Negative
234	Norfentanyl	Negative
234	Oxy		Positive
345	Fentanyl	Positive
345	Norfentanyl	Negative
345	Oxy		Positive
456	Fentanyl	Negative
456	Norfentanyl	Negative
456	Oxy		Positive
567	Fentanyl	Positive
567	Norfentanyl	Positive
567	Oxy		Positive
679	Fentanyl	Negative
679	Norfentanyl	Positive
679	Oxy		Negative
678	Fentanyl	Negative
678 	Norfentanyl	Positive
678	Oxy		Negative
789	Fentanyl	Positive
789	Norfentanyl	Positive
789	Oxy		Negative
890	Fentanyl	Negative
890	Norfentanyl	Positive
890	Oxy		Positive
134	Fentanyl	Negative
134	Norfentanyl	Negative
134	Oxy		Negative
245	Fentanyl	Positive
245	Norfentanyl	Positive
245	Oxy		Negative
356	Fentanyl	Negative
356	Norfentanyl	Negative
356	Oxy		Negative
467	Fentanyl	Negative
467	Norfentanyl	Positive
467	Oxy		Positive
578	Fentanyl	Negative
578	Norfentanyl	Negative
578	Oxy		Negative
690	Fentanyl	Positive
690	Norfentanyl	Positive
690	Oxy		Negative
321	Fentanyl	Negative
321	Norfentanyl	Negative
321	Oxy		Negative
;

Data LBCFin;
	set work.LB1;
run;

* Create SAS Dataset from Excel File;

OPTIONS COMPRESS = YES; 
options ls=70 ps=50 mergenoby=error;


proc format ;  
 value $RESULT_VAL 'Positive' = '1'
                   'Negative' = '0';
run;

data LBCFin1;
	set LBCFin;
   format RESULT_VAL $RESULT_VAL.; 
run;

* Printout;

proc print data = LBCFin1; run; 

* Transpose Wide Steps; 

proc sort tagsort data = LBCFin1;
      by ALIAS2 CE_EVENT_DISP DRAWN_DT_TM;
run;

proc transpose data = LBCFin1 out = LBConFin;
            by ALIAS2 CE_EVENT_DISP DRAWN_DT_TM;
            var RESULT_VAL;
run;

proc sort tagsort data = LBConFin; * ADDED THIS EXTRA PROC SORT, IT WORKED;
      by ALIAS2 DRAWN_DT_TM;
run;

proc transpose data = LBConFin delimiter=_ out=LBCFin3 (drop=_name_);
            by ALIAS2 DRAWN_DT_TM;
            var col1; 
            id CE_EVENT_DISP;
run;

proc print data=LBCFin3 (obs=200); run;

 proc contents data=LBCFin4; run;

 proc freq data=LBCFin4; tables Fentanyl Norfentanyl LBCFent LBCNorfent ; run; 
/******************* Changing drug class variables*************/;
/* Fix using variable names*/;

data LBCFin4;
 set LBCFin3;
 length LBCFent $50.; 
 format LBCFent $50.;
 if  strip(Fentanyl) = "1" or strip(Norfentanyl) = "1" then LBCFent = 'Pos';
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2019 14:50:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612416#M178695</guid>
      <dc:creator>Blndbrm727</dc:creator>
      <dc:date>2019-12-17T14:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612421#M178698</link>
      <description>&lt;P&gt;This is different data than you showed in reply #4 or reply #7. No wonder the code provided doesn't work. It is also very confusingly named, so that variable Fentanyl can have values Fentanyl or norfentanyl. But anyway, you never had numbers 0 or 1, you have character strings formatted to zero or one, and to do any type of logic where you test if a variable is equal to a value, you don't test if is equal to the formatted value (because that won't work), you test if it is equal to the unformatted value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, with this data, here is some code that will work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length mrn $16;
input MRN $ Fentanyl :$16. Norfentanyl :$16. ;
datalines;
123	Fentanyl	Positive
123	Norfentanyl 	Positive
123	Oxy		Negative
234	Fentanyl	Negative
234	Norfentanyl	Negative
234	Oxy		Positive
345	Fentanyl	Positive
345	Norfentanyl	Negative
345	Oxy		Positive
456	Fentanyl	Negative
456	Norfentanyl	Negative
456	Oxy		Positive
567	Fentanyl	Positive
567	Norfentanyl	Positive
567	Oxy		Positive
679	Fentanyl	Negative
679	Norfentanyl	Positive
679	Oxy		Negative
678	Fentanyl	Negative
678 	Norfentanyl	Positive
678	Oxy		Negative
789	Fentanyl	Positive
789	Norfentanyl	Positive
789	Oxy		Negative
890	Fentanyl	Negative
890	Norfentanyl	Positive
890	Oxy		Positive
134	Fentanyl	Negative
134	Norfentanyl	Negative
134	Oxy		Negative
245	Fentanyl	Positive
245	Norfentanyl	Positive
245	Oxy		Negative
356	Fentanyl	Negative
356	Norfentanyl	Negative
356	Oxy		Negative
467	Fentanyl	Negative
467	Norfentanyl	Positive
467	Oxy		Positive
578	Fentanyl	Negative
578	Norfentanyl	Negative
578	Oxy		Negative
690	Fentanyl	Positive
690	Norfentanyl	Positive
690	Oxy		Negative
321	Fentanyl	Negative
321	Norfentanyl	Negative
321	Oxy		Negative
;

proc sort data=have;
    by mrn;
run;

data want;
    merge have(where=(fentanyl='Fentanyl') rename=(norfentanyl=posneg1))
		have(where=(fentanyl='Norfentanyl') rename=(norfentanyl=posneg2));
	by mrn;
    if posneg1='Positive' or posneg2='Positive' then lgcfen='Pos';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 15:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612421#M178698</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-17T15:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Code for New Variables not producing output - Using If-Then statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612435#M178706</link>
      <description>&lt;P&gt;I can only say:&amp;nbsp;&lt;STRONG&gt;Maxim 3&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Know Your Data&lt;/STRONG&gt; includes assigned formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And comparisons always use the raw values, not the formatted ones.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 15:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-New-Variables-not-producing-output-Using-If-Then/m-p/612435#M178706</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-17T15:57:12Z</dc:date>
    </item>
  </channel>
</rss>

