<?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 Assign observations to a category in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-observations-to-a-category/m-p/809636#M319284</link>
    <description>&lt;P&gt;I have a table with observations and I want to assign those to specific categories based on rules.&lt;/P&gt;&lt;P&gt;My sample data is e.g. the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;	create table work.observations
	(id char(2),
	can_fly num,
	furry num,
	levitates num)
	;
	
	insert into work.observations
	values('AB', 0, 0, 0)
	values('AC', 1, 0, 0)
	values('AD', 0, 1, 0)
	values('BC', ., 1, 0)
	values('BD', ., 0, 0)
	;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I want to assign the species.&lt;/P&gt;&lt;P&gt;For this example, assume there are three possibilities, a penguin, sparrow, or a cat. If the animal is furry, I want to assign "cat". If it doesn't, it's a bird, which one is determined by whether or not it can fly. If I already know it's a cat, I don't care about the value of the "can_fly" column.&lt;/P&gt;&lt;P&gt;I could do this for one species at a time in the following way:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let species = cat;
%let furry = (1);
%let can_fly = (0, 1, .);
%let levitates = (0);

proc sql;
	create table work.assigned_observations as
	select distinct
		o.id,
		o.can_fly,
		o.furry,
		o.levitates,
		case when o.can_fly in &amp;amp;can_fly.
				and o.furry in &amp;amp;furry.
				and o.levitates in &amp;amp;levitates.
			then "&amp;amp;species." end as species
	from work.observations o 
	;
quit;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, I want to do this for all at once. I was thinking of writing the possible settings to a file (e.g. excel), then loading this into a table and perform a join. However, I haven't been able to get the possibilities as lists as in the above example, so the best I could get working includes string comparisons, which is inefficient. See the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table work.species
	(species char(10),
	can_fly char(10),
	furry char(10),
	levitates char(10))
	;
	
	insert into work.species
	values('penguin', '(0)', '(0)', '(0)')
	values('cat', '(0, 1, .)', '(1)', '(0)')
	values('sparrow', '(1)', '(0)', '(0)')
	;
	
	title 'Different species'
	
	select * from work.species
proc printto; run;

proc sql;
	create table work.assigned_observations as
	select distinct 
		o.id,
		o.can_fly,
		o.furry,
		o.levitates,
		s.species
	from work.observations o
	left join work.species s
		on s.can_fly like catx('', '%', put(o.can_fly, 11.), '%')
		and s.furry like catx('', '%', put(o.furry, 11.), '%')
		and s.levitates like catx('', '%', put(o.levitates, 11.), '%')
	;
quit;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How can I improve this, while keeping the flexibility of having multiple options and keeping the input file (with the settings for the different species) readable?&lt;/P&gt;</description>
    <pubDate>Mon, 25 Apr 2022 09:32:47 GMT</pubDate>
    <dc:creator>vlapoutre</dc:creator>
    <dc:date>2022-04-25T09:32:47Z</dc:date>
    <item>
      <title>Assign observations to a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-observations-to-a-category/m-p/809636#M319284</link>
      <description>&lt;P&gt;I have a table with observations and I want to assign those to specific categories based on rules.&lt;/P&gt;&lt;P&gt;My sample data is e.g. the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;	create table work.observations
	(id char(2),
	can_fly num,
	furry num,
	levitates num)
	;
	
	insert into work.observations
	values('AB', 0, 0, 0)
	values('AC', 1, 0, 0)
	values('AD', 0, 1, 0)
	values('BC', ., 1, 0)
	values('BD', ., 0, 0)
	;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I want to assign the species.&lt;/P&gt;&lt;P&gt;For this example, assume there are three possibilities, a penguin, sparrow, or a cat. If the animal is furry, I want to assign "cat". If it doesn't, it's a bird, which one is determined by whether or not it can fly. If I already know it's a cat, I don't care about the value of the "can_fly" column.&lt;/P&gt;&lt;P&gt;I could do this for one species at a time in the following way:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let species = cat;
%let furry = (1);
%let can_fly = (0, 1, .);
%let levitates = (0);

proc sql;
	create table work.assigned_observations as
	select distinct
		o.id,
		o.can_fly,
		o.furry,
		o.levitates,
		case when o.can_fly in &amp;amp;can_fly.
				and o.furry in &amp;amp;furry.
				and o.levitates in &amp;amp;levitates.
			then "&amp;amp;species." end as species
	from work.observations o 
	;
quit;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, I want to do this for all at once. I was thinking of writing the possible settings to a file (e.g. excel), then loading this into a table and perform a join. However, I haven't been able to get the possibilities as lists as in the above example, so the best I could get working includes string comparisons, which is inefficient. See the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table work.species
	(species char(10),
	can_fly char(10),
	furry char(10),
	levitates char(10))
	;
	
	insert into work.species
	values('penguin', '(0)', '(0)', '(0)')
	values('cat', '(0, 1, .)', '(1)', '(0)')
	values('sparrow', '(1)', '(0)', '(0)')
	;
	
	title 'Different species'
	
	select * from work.species
proc printto; run;

proc sql;
	create table work.assigned_observations as
	select distinct 
		o.id,
		o.can_fly,
		o.furry,
		o.levitates,
		s.species
	from work.observations o
	left join work.species s
		on s.can_fly like catx('', '%', put(o.can_fly, 11.), '%')
		and s.furry like catx('', '%', put(o.furry, 11.), '%')
		and s.levitates like catx('', '%', put(o.levitates, 11.), '%')
	;
quit;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How can I improve this, while keeping the flexibility of having multiple options and keeping the input file (with the settings for the different species) readable?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2022 09:32:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-observations-to-a-category/m-p/809636#M319284</guid>
      <dc:creator>vlapoutre</dc:creator>
      <dc:date>2022-04-25T09:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Assign observations to a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-observations-to-a-category/m-p/809649#M319292</link>
      <description>&lt;P&gt;A simpler way may be to keep a column in SPECIES missing, if you do not care about it for this species, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	create table work.species
	(species char(10),
	can_fly num,
	furry num,
	levitates num)
	;
	
	insert into work.species
	values('penguin', 0,0,0)
	values('cat', .,1,0)
	values('sparrow', 1,0,0)
	;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You should then be able to match like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	create table assigned_observations as
	select observations.*,species.species
	from observations left join species
	on (observations.furry=species.furry or species.furry is null) and
	   (observations.can_fly=species.can_fly or species.can_fly is null) and
	   (observations.levitates=species.levitates or species.levitates is null)
	   ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you only want the observations that can actually be assigned to a species, just leave out the "left" in the final SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2022 10:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-observations-to-a-category/m-p/809649#M319292</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-04-25T10:59:33Z</dc:date>
    </item>
  </channel>
</rss>

