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

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 119 views
  • 0 likes
  • 2 in conversation