<?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: If statement on variables with the same prefix and conditional replacement between variables in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892619#M39709</link>
    <description>&lt;P&gt;Never say "it doesn't work" and provide no further information. We don't know what you did; we don't know what happened.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Always show us the code you used. Always show us (a portion of) the data set as WORKING data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;). If there are errors in the log, provide the ENTIRE log for this PROC, not just the error messages.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Sep 2023 18:27:49 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-09-04T18:27:49Z</dc:date>
    <item>
      <title>If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892587#M39698</link>
      <description>&lt;P&gt;Hi all suppose to have the following two cases:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset1:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;ID   Condition    N_data_exp     N_data_dis    N_data_reg
 A        2           23             54             65
 B        1           27             23             67
 C        5           32             43             78
 D        3           2              23             70
 E        8           87             1              4
..       ..           ..             ..             ..&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I would replace all values &amp;lt; 5 in columns starting with "N_" with 999. I used the&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; following code but it does not work. Of course I'm doing something wrong:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;data output; 
  set dataset1; 
  if N_: &amp;lt; 5 then N_: = 999; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;I cannot use the condition "numeric" because there is the variable "Condition" that must not change.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the second case is the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; ID   Condition    N_data_exp     N_data_dis    N_data_reg    data_exp_res  data_dis_res   ....
 A        2           23             54             65            345           34
 B        1           27             23             67            52            987
 C        5           32             43             78            675           321
 D        3           2              23             70            0.2           41
 E        8           87             1              4             34            0.001
..       ..           ..             ..             ..&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;..&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;..&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would replace all values &amp;lt; 5 in columns starting with "N_" with 999 in corresponding columns ending with "_res".&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please? I have to manage the two cases separately.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really thank you in advance&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892587#M39698</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T16:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892592#M39699</link>
      <description>&lt;P&gt;Are these numeric variables? If so, they cannot have the value "Outlier", that is character. Cannot be done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can DISPLAY values less than 5 as some text string ... their value will still be a number, but it is displayed as Outlier. Would that work for you? The only reason I can think of for putting Outlier into these fields is to display the table for people.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value outlf low-5='Outlier';
run;
proc print data=have;
    var id condition n_:;
    format n_: outlf.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: if you do arithmetic on these variables, such as computing a sum, a number less than 5 will still be that number.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892592#M39699</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T16:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892594#M39700</link>
      <description>I will edit the post. Instead of "outlier" also 999 is ok</description>
      <pubDate>Mon, 04 Sep 2023 16:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892594#M39700</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T16:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892595#M39701</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I will edit the post. Instead of "outlier" also 999 is ok&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;NOOOOOO! NOOOOOO! NOOOOOO! NOOOOOO!&lt;/P&gt;
&lt;P&gt;NOOOOOO! NOOOOOO! NOOOOOO! NOOOOOO!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should never put a potentially valid number into a dataset to indicate outlier. Then any sum or average or other computation will not be correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please discuss the purpose of doing this. Is it to display a table for people? Is it for some other reason? If so, what?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;What is wrong with the PROC FORMAT solution I gave?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892595#M39701</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T16:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892596#M39702</link>
      <description>&lt;P&gt;Nothing wrong with proc format. No computations will be done on the tables. They result from computations already. Will be only displayed.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892596#M39702</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T16:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892597#M39703</link>
      <description>&lt;P&gt;Then PROC FORMAT is the solution I would recommend.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892597#M39703</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T16:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892599#M39704</link>
      <description>And what about the case of dataset2?</description>
      <pubDate>Mon, 04 Sep 2023 16:49:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892599#M39704</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T16:49:36Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892602#M39705</link>
      <description>&lt;P&gt;So, if I am understanding you properly, you want only to apply this format to variables in DATASET2 whose name ends with a particular suffix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus, you need a list of those variables. PROC SQL can create that list (as the macro variable &amp;amp;names, by finding names that end in _RES)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    select name into :names separated by ' ' from dictionary.columns
    where reverse(trim(name)) eqt 'ser_' and libname='WORK' and memname='DATASET2';
quit;
%put &amp;amp;=names;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=dataset2;
     format &amp;amp;names outf.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 17:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892602#M39705</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T17:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892603#M39706</link>
      <description>Yes, so the condition should be verified in columns starting with N_ while the replacement should be done in columns (corresponding!!!!) ending with _res</description>
      <pubDate>Mon, 04 Sep 2023 17:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892603#M39706</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T17:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892615#M39707</link>
      <description>Unfortunately it does not work. The output of proc sql is empty</description>
      <pubDate>Mon, 04 Sep 2023 18:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892615#M39707</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2023-09-04T18:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892618#M39708</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, so the condition should be verified in columns starting with N_ while the replacement should be done in columns (corresponding!!!!) ending with _res&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you want to make NEW variables with names that are derived from the names of other variables?&lt;/P&gt;
&lt;P&gt;You can use an SQL query to help with that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So get a list of the variables in your dataset. The easiest way is to use PROC CONTENTS.&amp;nbsp; So if your existing dataset is named HAVE you can make a dataset named CONTENTS with the names (and other metadata) for the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents noprint data=HAVE out=CONTENTS;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use that dataset to find the variables you want and also generate their names into macro variables by using PROC SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nliteral(name)
     , nliteral(cats(substr(name,3),'_res'))
  into :old separated by ' '
     , :new separated by ' '
  from contents
  where upcase(name) eqt 'N_'
    and type='num'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use those two macro variables to define arrays to allow you to run the same code for many variable pairs by indexing into the arrays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then use a DO loop to iterate an index variable over the number of variables in the array and do what ever calculations you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you are trying to HIDE the values of the NEW list when the values in the OLD list are less than 5.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you might do something like this to replace such values with special missing .B (B for blinded).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array old &amp;amp;old;
  array new &amp;amp;new;
  do index=1 to dim(old);
      if 0 &amp;lt; old[index] &amp;lt; 5 then new[index]=.B ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 18:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892618#M39708</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-04T18:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: If statement on variables with the same prefix and conditional replacement between variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892619#M39709</link>
      <description>&lt;P&gt;Never say "it doesn't work" and provide no further information. We don't know what you did; we don't know what happened.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Always show us the code you used. Always show us (a portion of) the data set as WORKING data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;). If there are errors in the log, provide the ENTIRE log for this PROC, not just the error messages.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 18:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-statement-on-variables-with-the-same-prefix-and-conditional/m-p/892619#M39709</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T18:27:49Z</dc:date>
    </item>
  </channel>
</rss>

