<?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: PROC SQL Case when statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563021#M157786</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQl;
create table want as 

	select distinct CARD_NUM, 
	case 
		when 	count( distinct scan(STORE_NAME,1,'_')) &amp;gt; 1
		 then "UNCLASSIFIED"
		 else	cats(scan(STORE_NAME,1,'_'),'_',"ONLY")
	end as class
	from c
	group by CARD_NUM;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 31 May 2019 21:02:07 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2019-05-31T21:02:07Z</dc:date>
    <item>
      <title>PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562963#M157774</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have PAN INDIA customer shopping table which contains online transaction details as well as store details.&lt;/P&gt;
&lt;P&gt;I want to create a new variable for category like customer is online_only, Store_only and unclassified (if shopped by both criteria).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample dataset :&lt;/P&gt;
&lt;P&gt;DATA C;&lt;BR /&gt;input CARD_NUM $5. STORE_NAME $7.;&lt;BR /&gt;CARDS;&lt;BR /&gt;12345 ONLINE&lt;BR /&gt;23456 STR_1&lt;BR /&gt;98765 ONLINE&lt;BR /&gt;98765 STR_NM&lt;BR /&gt;98765 STR_5&lt;BR /&gt;43214 STR_A&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output :&lt;/P&gt;
&lt;P&gt;12345 ONLINE_ONLY&lt;BR /&gt;23456 STORE_ONLY&lt;BR /&gt;98765 UNCLASSIFIED&lt;BR /&gt;43214 STORE_ONLY&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;kindly note : For online transaction, i have only one indicator as 'ONLINE' but for store visit we have n number of store names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking for your kind support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Uma Shanker Saini&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2019 18:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562963#M157774</guid>
      <dc:creator>umashankersaini</dc:creator>
      <dc:date>2019-05-31T18:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562995#M157779</link>
      <description>calc min and max store name (just 1st 3 letters)   per card.  &lt;BR /&gt;if the min and max are the same, use it for output otherwise 'unclassified'.</description>
      <pubDate>Fri, 31 May 2019 19:52:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562995#M157779</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-05-31T19:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562997#M157780</link>
      <description>&lt;P&gt;You need to explain the rules of how STORE_NAME will map to your new categories.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46512"&gt;@umashankersaini&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have PAN INDIA customer shopping table which contains online transaction details as well as store details.&lt;/P&gt;
&lt;P&gt;I want to create a new variable for category like customer is online_only, Store_only and unclassified (if shopped by both criteria).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample dataset :&lt;/P&gt;
&lt;P&gt;DATA C;&lt;BR /&gt;input CARD_NUM $5. STORE_NAME $7.;&lt;BR /&gt;CARDS;&lt;BR /&gt;12345 ONLINE&lt;BR /&gt;23456 STR_1&lt;BR /&gt;98765 ONLINE&lt;BR /&gt;98765 STR_NM&lt;BR /&gt;98765 STR_5&lt;BR /&gt;43214 STR_A&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output :&lt;/P&gt;
&lt;P&gt;12345 ONLINE_ONLY&lt;BR /&gt;23456 STORE_ONLY&lt;BR /&gt;98765 UNCLASSIFIED&lt;BR /&gt;43214 STORE_ONLY&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;kindly note : For online transaction, i have only one indicator as 'ONLINE' but for store visit we have n number of store names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking for your kind support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Uma Shanker Saini&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2019 19:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/562997#M157780</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-31T19:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563012#M157782</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46512"&gt;@umashankersaini&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you don't need a CASE WHEN statement here. Just use the MAX and CHOOSEC functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select card_num, choosec(max(store_name='ONLINE')+2*max(store_name ne 'ONLINE'),
                         'ONLINE_ONLY', 'STORE_ONLY', 'UNCLASSIFIED')
                 as categ length=12
from c
group by card_num;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And, yes, as&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;/P&gt;
&lt;P&gt;You need to explain the rules of how STORE_NAME will map to your new categories.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code is based on the assumption that &lt;EM&gt;anything other than&lt;/EM&gt; 'ONLINE' (what about spelling variants like 'Online'? Missing values?) indicates "Store". Obviously, you would need to modify the conditions about STORE_NAME if this assumption does not hold in your real data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to preserve the order of CARD_NUM values from dataset C, I'd rather recommend a DATA step solution, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_: store_name);
do until(last.card_num);
  set c;
  by card_num notsorted;
  if store_name='ONLINE' then _o=1;
  else _s=1;
end;
length categ $12;
categ=choosec(sum(_o,_s,_s), 'ONLINE_ONLY', 'STORE_ONLY', 'UNCLASSIFIED');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Same assumption as above plus the data must be grouped by CARD_NUM.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2019 20:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563012#M157782</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-31T20:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563013#M157783</link>
      <description>&lt;P&gt;This works for the provided example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $store_online
'ONLINE' = 'ONLINE'
other    = 'STORE'
;

DATA C;
input CARD_NUM $5. STORE_NAME $7.;
CARDS;
12345 ONLINE
23456 STR_1
98765 ONLINE
98765 STR_NM
98765 STR_5
43214 STR_A
;
Run;

proc freq data=c noprint;
   tables card_num*store_name/out=cardcount;
   format store_name $store_online.;
run;

proc sql;
   create table want as
   select distinct card_num,
      case when count(*)&amp;gt;1 then 'UNCLASSIFIED'
           When count(*)=1 then put(store_name,$store_online.)
     end as status
   from cardcount
   group by card_num
   ;
quit;

&lt;/PRE&gt;
&lt;P&gt;The Format plus Proc freq reduces the possible values of store to 2.Formatted values will be used to create groups in the proc. If the summary output from proc freq for a card has more than one row (count(*)&amp;gt;1) then it should come from combination of store and online.&lt;/P&gt;
&lt;P&gt;I didn't make the format use the longer text you show. That is left as an exercise for the interested reader.&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2019 20:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563013#M157783</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-31T20:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563015#M157784</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;Create table want as select distinct Card_Num ,&lt;BR /&gt;case when (count(Store_Name) = 1 and lower(Store_Name) like 'str%') then 'Store_Only'&lt;BR /&gt;when (count(Store_Name) = 1 and lower(Store_name) ='online') then 'Online'&lt;BR /&gt;when (count(Store_Name) &amp;gt;= 2) then 'Unclassified' end as Category&lt;BR /&gt;from C&lt;BR /&gt;group by Card_Num&lt;BR /&gt;Order by Card_Num&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output would be :&lt;/P&gt;&lt;P&gt;CARD_NUM Category&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;Online&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23456&lt;/TD&gt;&lt;TD&gt;Store_Only&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;43214&lt;/TD&gt;&lt;TD&gt;Store_Only&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;98765&lt;/TD&gt;&lt;TD&gt;Unclassified&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anushree&lt;/P&gt;</description>
      <pubDate>Fri, 31 May 2019 20:47:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563015#M157784</guid>
      <dc:creator>anushreebiotech</dc:creator>
      <dc:date>2019-05-31T20:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563021#M157786</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQl;
create table want as 

	select distinct CARD_NUM, 
	case 
		when 	count( distinct scan(STORE_NAME,1,'_')) &amp;gt; 1
		 then "UNCLASSIFIED"
		 else	cats(scan(STORE_NAME,1,'_'),'_',"ONLY")
	end as class
	from c
	group by CARD_NUM;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 May 2019 21:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563021#M157786</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-05-31T21:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563075#M157801</link>
      <description>&lt;P&gt;Hi Team,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your quick reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no pattern for store_name, if offline (store visit). we have pattern for online purchase only.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried with :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;Create table want (compress=yes) as &lt;BR /&gt;Select distinct CARD_NUM&lt;BR /&gt;, case when (STORE_NAME) like '%ONLINE%' then 'ONLINE ONLY'&lt;BR /&gt;when STORE_NAME not like '%ONLINE%' then 'STORE ONLY'&lt;BR /&gt;when STORE_NAME like '%ONLINE%' and STORE_NAME not like '%ONLINE%' then 'UNCLASSIFIED'&lt;BR /&gt;End as CHANNEL&lt;BR /&gt;From A&lt;BR /&gt;Group by 1;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is not working fine for unclassified category&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2019 08:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563075#M157801</guid>
      <dc:creator>umashankersaini</dc:creator>
      <dc:date>2019-06-01T08:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563076#M157802</link>
      <description>&lt;P&gt;1. If customer is buying product online - then online only&lt;/P&gt;
&lt;P&gt;2. If customer is buying by going to physical store - then store only&lt;/P&gt;
&lt;P&gt;3. if customer is buying by both ways (online and store visit ) - Then unclassified&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for online purchase - store_name would be same like 'ONLINE' across the globe&lt;/P&gt;
&lt;P&gt;for physical store - Store_name would be any thing without pattern.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2019 08:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563076#M157802</guid>
      <dc:creator>umashankersaini</dc:creator>
      <dc:date>2019-06-01T08:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563078#M157803</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46512"&gt;@umashankersaini&lt;/a&gt;: Thanks for clarifying the criteria. Have you tried to implement these in my suggested solution? If not, please see below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select card_num, choosec(max(store_name like '%ONLINE%')+2*max(store_name not like '%ONLINE%'),
                         'ONLINE_ONLY', 'STORE_ONLY', 'UNCLASSIFIED')
                 as channel length=12
from c
group by card_num;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Jun 2019 09:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Case-when-statement/m-p/563078#M157803</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-06-01T09:28:41Z</dc:date>
    </item>
  </channel>
</rss>

