BookmarkSubscribeRSS Feed
cho16
Obsidian | Level 7

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;

2 REPLIES 2
PGStats
Opal | Level 21

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
Ksharp
Super User
Post some data to let us test it . and post the output you want to see.


SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 861 views
  • 0 likes
  • 3 in conversation