<?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: Creating a variable that combines same levels of existing variables  SAS 9.4 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927361#M364969</link>
    <description>&lt;P&gt;Here is what I think you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You apparently want to create a new variable CLINIC, such that&lt;BR /&gt;&amp;nbsp; &amp;nbsp; If&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; all the non-blank values for clinic_name1, clinic_name2, clinic_name3 have the same value,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; all the corresponding prior_span values are "1"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; the new variable gets the common clinic_name value&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; it is left blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=i);
  set have;


  array cn {3} $ clinic_name_1- clinic_name_3;
  array ps {3} $ prior_span1 - prior_span3;

  if prior_span1='1' then clinic=clinic_name_1;

  do i=2 to 3 while (cn{i}^=' ');
    if cn{i}^=cn{1} or ps{i}^='1' then clinic=' ';
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This rule, in the case of your sample data, generates a name for only 1 observation (out of 12).&amp;nbsp; So I think we may need more clarity from you on the rule to use in assigning a value to CLINIC.&lt;/P&gt;</description>
    <pubDate>Tue, 07 May 2024 22:16:19 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-05-07T22:16:19Z</dc:date>
    <item>
      <title>Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927356#M364966</link>
      <description>&lt;P&gt;I need to combine &lt;EM&gt;same&lt;/EM&gt; clinic names if the corresponding prior_span =1.&lt;/P&gt;
&lt;P&gt;For example, if Clinic_name1 and Clinic_name2 where both Clinic A and Prior_span1 and prior_span2 both =1 then Clinic would = A.&lt;/P&gt;
&lt;P&gt;Clinic_name_1 corresponds with Prior_span1&lt;/P&gt;
&lt;P&gt;Clinic_name_2 corresponds with Prior_span2&lt;/P&gt;
&lt;P&gt;Clinic_name_3 corresponds with Prior_span3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is my sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
input 

ID$	Clinic_Name_1$	Clinic_Name_2$	Clinic_Name_3$	Prior_Span1$	Prior_Span2$	Prior_Span3$	Cost service$ ;

datalines;
1	B	A	.	0	0	0	168 A
2	A	A	.	1	0	0	46  B
2	A	A	.	0	1	0	99  C
3	B	.	.	0	0	0	24  D
4	D	.	.	0	0	0	54  A
5	B	D	F	0	1	0	405 B
7	A	.	.	1	0	0	90  F
8	E	E	A	0	0	1	87  J
9	F	.	.	0	0	0	0   G
10	C	C	C	0	0	0	40  C
10	C	C	C	0	0	1	374 B
10	C	C	C	0	1	1	60  A

;

run;




***Summary table-clinic var not created yet****;
proc sql;
create table want as

select 1 as ID1,'service' as a length=40,Clinic,
       1 as ID2,put(service, $40.) as b length=40,
	   1 as ID3,(select count(distinct ID) from have where Clinic=a.Clinic and service=a.service and cost&amp;gt;0 ) as c format=comma20.,
	   1 as ID4,calculated c/(select count(distinct ID) from have where Clinic=a.Clinic and cost&amp;gt;0 ) as d format=percent8.1,
	   1 as ID5,sum(cost) as e format=dollar20.,
	   1 as ID6,sum(cost)/(select sum(cost) from have where Clinic=a.Clinic) as f format=percent8.1

 from have as a

  group by Clinic,service
union all
select 7 as id1,'09'x as a,Clinic,
       7 as id2,'Total Clients' as b,
	   7 as id3,count(distinct ID) as f,
	   7 as id4,.,
	   7 as id5,.,
	   7 as id7,.

 from have
where service ne 'j'  and cost&amp;gt;0
  group by Clinic
union all
select 7 as id1,'09'x as a,Clinic,
       7 as id2,' Total Cost' as f,
	   7 as id3,. as c,
	   7 as id4,.,
	   7 as id5,sum(cost),
	   7 as id7,.

 from have
where service ne 'j'  and cost&amp;gt;0
      group by Clinic
;quit;
options missing=' ';

proc report data=want nowd;

column id1 a b Clinic,(c d e f);
define id1/group noprint;
define a/group noprint ' ';
define b/group ' ';
define c/analysis 'Clients';
define d/analysis '% of Clients';
define e/analysis 'Cost';
define f/analysis '% of Cost';

define Clinic/across '';
compute before a/style={just=l color=black};
if id1=1 then len=0;
 else len=10;
x=' ';
line x $varying10. len;
line a $40.;
endcomp;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2024 21:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927356#M364966</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-05-07T21:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927357#M364967</link>
      <description>&lt;P&gt;Thank you for providing working data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you provide an example of what the data looks like after the "same levels" are combined for that data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure what the output should look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should expand on your single example to include a few more. Such as what are the rules for when you have two (or more) Prior_span =1 and correspond to different clinics such as hypothetical:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;15	A	B	C	1	1	1	60  A&lt;/LI-CODE&gt;
&lt;P&gt;what is the rule for selecting the response?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the output where there are no 1 in any of the Prior_span&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2024 21:43:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927357#M364967</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-07T21:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927361#M364969</link>
      <description>&lt;P&gt;Here is what I think you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You apparently want to create a new variable CLINIC, such that&lt;BR /&gt;&amp;nbsp; &amp;nbsp; If&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; all the non-blank values for clinic_name1, clinic_name2, clinic_name3 have the same value,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; all the corresponding prior_span values are "1"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; the new variable gets the common clinic_name value&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; it is left blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=i);
  set have;


  array cn {3} $ clinic_name_1- clinic_name_3;
  array ps {3} $ prior_span1 - prior_span3;

  if prior_span1='1' then clinic=clinic_name_1;

  do i=2 to 3 while (cn{i}^=' ');
    if cn{i}^=cn{1} or ps{i}^='1' then clinic=' ';
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This rule, in the case of your sample data, generates a name for only 1 observation (out of 12).&amp;nbsp; So I think we may need more clarity from you on the rule to use in assigning a value to CLINIC.&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2024 22:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927361#M364969</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-05-07T22:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927566#M365010</link>
      <description>&lt;P&gt;Sorry, I did a poor job of explain this. Ultimately, I want to sum clients and cost by Clinic and Service where prior span =1. Here is an Excel example of what I am trying to do in SAS.&amp;nbsp; I have added ballardw's example to the sample data set.&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;Hope this helps!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96346iAE449CFFEAE99E07/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 17:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927566#M365010</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-05-08T17:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927582#M365016</link>
      <description>&lt;P&gt;Photographs of a spreadsheet without any explanation do not help much.&lt;/P&gt;
&lt;P&gt;What are the RULES?&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 18:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927582#M365016</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-08T18:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that combines same levels of existing variables  SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927619#M365028</link>
      <description>&lt;P&gt;I think the code(wrote by me) you posted has nothing to do with your question.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
input 
ID$	Clinic_Name_1$	Clinic_Name_2$	Clinic_Name_3$	Prior_Span1$	Prior_Span2$	Prior_Span3$	Cost service$ ;
datalines;
1	B	A	.	0	0	0	168 A
2	A	A	.	1	0	0	46  B
2	A	A	.	0	1	0	99  C
3	B	.	.	0	0	0	24  D
4	D	.	.	0	0	0	54  A
5	B	D	F	0	1	0	405 B
7	A	.	.	1	0	0	90  F
8	E	E	A	0	0	1	87  J
9	F	.	.	0	0	0	0   G
10	C	C	C	0	0	0	40  C
10	C	C	C	0	0	1	374 B
10	C	C	C	0	1	1	60  A
15  A   B   C   1   1   1   60  A
;
run;
proc sql;
create table classdata as
select * from 
(
select distinct Clinic_Name_1 from have where Clinic_Name_1 is not missing
union
select distinct Clinic_Name_2 from have where Clinic_Name_1 is not missing
union
select distinct Clinic_Name_3 from have where Clinic_Name_1 is not missing
),
(select distinct service from have where service is not missing)
;

create table want as
select Clinic_Name_1,service,count(*) as count,sum(cost) as cost from have where Prior_Span1='1' group by Clinic_Name_1,service
union all
select Clinic_Name_2,service,count(*) as count,sum(cost) as cost from have where Prior_Span2='1' group by Clinic_Name_2,service
union all
select Clinic_Name_3,service,count(*) as count,sum(cost) as cost from have where Prior_Span3='1' group by Clinic_Name_3,service
;
quit;
proc tabulate data=want classdata=classdata;
class service Clinic_Name_1;
var count cost;
table service all,(Clinic_Name_1='Clinic' all)*(count*f=f20.0 cost*f=dollar22.0);
keylabel sum=' ' all='Total';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1715219613683.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96362iDEAE65CE1B2D594F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1715219613683.png" alt="Ksharp_0-1715219613683.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 01:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-combines-same-levels-of-existing/m-p/927619#M365028</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-05-09T01:53:54Z</dc:date>
    </item>
  </channel>
</rss>

