2

I have a dataset in SAS called "Flight" and i want to label all the rows of one particular column "Carrier" with values which matches ("Flght_carrier_code") from another dataset called "Airlines".

Please suggest some method.

Sample Data set 1 - "Flight"

date      carrier   flight  tailnum air_time
01-01-2013  UA      1714    N24211  227
01-01-2013  AA      1141    N619AA  160
01-01-2013  B6       725    N804JB  183
01-01-2013  DL      461     N668DN  116
01-01-2013  UA      1696    N39463  150
01-01-2013  B6      507     N516JB  158
01-01-2013  EV      5708    N829AS  53
01-01-2013  B6      79      N593JB  140
01-01-2013  AA      301     N3ALAA  138
01-01-2013  B6      49      N793JB  149
01-01-2013  B6      71      N657JB  158

Sample Data set 2 - "Airlines"

Flght_carrier_Code  name
9E              Endeavor Air Inc.
AA              American Airlines Inc.
AS              Alaska Airlines Inc.
B6              JetBlue Airways
DL              Delta Air Lines Inc.
EV              ExpressJet Airlines Inc.
F9              Frontier Airlines Inc.
FL              AirTran Airways Corporation
HA              Hawaiian Airlines Inc.
MQ              Envoy Air
OO              SkyWest Airlines Inc.
UA              United Air Lines Inc.
US              US Airways Inc.
VX              Virgin America
WN              Southwest Airlines Co.
YV              Mesa Airlines Inc.
Aman kashyap
  • 143
  • 1
  • 3
  • 12
  • 1
    Doesn't sound like it has anything to do with adding a label to either a dataset or a variable. Instead if looks like you want to decode a variable. What have you tried? Did you create a format? – Tom May 15 '18 at 03:57
  • 1
    joins? Do you want a column with the name associated to the code? Then, use joins! – samkart May 15 '18 at 08:34

2 Answers2

7

The labelling you describe can be considered a row labeling, but more common terminology is:

  • value mapping
  • formatting
  • left join
  • merge
  • lookup

Note: SAS format is like an automatic within system left-join.

SQL

You tagged proc-sql so one approach is a left-join which would retain rows and carrier values that do not have a match. You might also want an sql view to prevent creating a newer larger table

proc sql;
  create view work.flights_v as
  select 
    coalesce(airlines.name, flights.carrier) as carrier_name
  , flights.*
  from 
    flights
  left join 
    airlines
  on
    flights.carrier = airlines.Flght_carrier_Code
  ;

FORMATS

Custom formats are typically involved with a variables data presentation level processing at viewing and output rendering time -- for example: EG grid, ViewTable, a procedures output. A custom format can be created from a data set such as airlines. Custom formats can permanent (persist after a SAS session ends) or temporary (exist only during a SAS session). Read your documentation about Proc FORMAT CNTLIN= if you want to try that way.

Richard
  • 25,390
  • 3
  • 25
  • 38
2

I would just add label to that code above

proc sql;
  create view work.flights_v as
  select 
    coalesce(airlines.name, flights.carrier) as carrier_name label="carrier"
  , flights.*
  from 
    flights
  left join 
    airlines
  on
    flights.carrier = airlines.Flght_carrier_Code
  ;
Reeza
  • 20,510
  • 4
  • 21
  • 38
Wired604
  • 370
  • 1
  • 3
  • 10