DATA Step, Macro, Functions and more

Code help

Reply
Contributor
Posts: 33

Code help

Hi ,

 

We have a table1 which contains marketname ,year  fields that has the data from the year 2014 to 2016.

 

Currently we are pulling data from table1 for the year 2016. While we join with this table 1 to get marketname for 2016 and its not matching. But if there is no matching, please match by previous year names table, if still not, use previous previous year

 

For example, if 2016 name not match, then use 2015, if still not, use 2014.

 

Please let me know how to go back for previous years and get the names.

 

Code:

 

proc sql;

create table QHPNOTEPtable_1 as

select Q.*,M.market_name from QHPNOTEPtable Q

left join table1 M ON Q.grPnum= M.group_ID;

quit;

Respected Advisor
Posts: 4,919

Re: Code help

Try to build something like this:

 

proc sql;
create table QHPNOTEPtable_1 as
select 
	Q.*,
	coalesce(M0.market_name, M1.market_name, M2.market_name) as market_name 
from 
	QHPNOTEPtable as Q left join 
	table1 as M0 on Q.grPnum= M0.group_ID left join
	table1 as M1 on Q.grPnum= M1.group_ID left join
	table1 as M2 on Q.grPnum= M2.group_ID
where 
	M0.year=2016 and 
	M1.year=2015 and 
	M2.year=2014;
quit;
PG
Super User
Posts: 10,020

Re: Code help

Post some data to let us test it . and post the output you want to see.


Ask a Question
Discussion stats
  • 2 replies
  • 206 views
  • 0 likes
  • 3 in conversation