BookmarkSubscribeRSS Feed
joon1
Quartz | Level 8

Dear Madam/Sir,

I would like to add conditions when two datasets are merged.

That is, how can we add conditions 'when cartel_byear=<cyear<=cartel_eyear then cartel_year=1; else cartel_year=0' when c2 (firm-year population) and ca3 are merged.

 

proc sql undo_policy=none;
create table c3 as select
c2.*,ca3.cartel_byear, ca3.cartel_eyear
from c2 left join ca3 on
when cartel_byear=<cyear<=cartel_eyear then cartel_year=1; else cartel_year=0;
(c2.gvkey=ca3.gvkey) ;
quit;

 

dataset ca3

Obs gvkey cartel_byear cartel_eyear12345
17869820002006
2154220042013
2154220032014
2154220042014
100419871995

dataset c2 (firm-year population)

Obs GVKEY FYEAR12345
0010001970
0010001971
0010001972
0010001973
0010001974

 

Any help will be highly appreciated.

 

Sincerely,

Joon

1 REPLY 1
Patrick
Opal | Level 21

If you want us to provide tested code then please provide your sample data in the form of a working SAS data step. Take the discussion here as an example how to provide such information.

 

If I understand right then you want to create a new variable with a value populated on some condition. That would be done in the SELECT statement via a CASE expression.
Docu for CASE is here.

 

Below code not tested.

proc sql undo_policy=none;
  create table c3 as 
    select
      c2.*
      ,ca3.cartel_byear 
      ,ca3.cartel_eyear
      ,case
        when ca3.cartel_byear=<cyear<=ca3.cartel_eyear then 1 
        else 0
        end as cartel_year
    from c2 left join ca3 
      on c2.gvkey=ca3.gvkey
  ;
quit;

From the data provided it was unclear to me from which source table cyear is coming. You will eventually have to add the table alias to it for things to work. If the variable only exists in one of the tables then things will also work without that alias - but it's cleaner to always add it. 
ca3.cartel_byear=<cyear<=ca3.cartel_eyear

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 341 views
  • 0 likes
  • 2 in conversation