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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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