BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pdhokriya
Pyrite | Level 9

Hi Reader,

 

How to union/set , when same column has different formats.

 1st dataset has - Sponsor records which is converted to Discontinued.

2nd dataset has - Sponsor which I required as it is.

 

proc format;
value $ fmt
'COMPLETED' = 'Completed'

"SPONSOR" = 'Discontinued'

;

run;

 

So in final output - required.

 

Discontinued

SPONSOR

 

- Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

You want to stack data so the formatted value is different based on which table the value came from.  However, Proc SQL will assign a column the format that is from the first table in the UNION and a column can have only one format.

 

Proc FORMAT maps a raw value to a single formatted value. (And, yes, a multi-label format (MLF) can map a single value to more than one value and some Procedures are MLF 'aware')

 

In your case MLF is not plausible, leaving you with a conundrum.

 

You will need some data transformation to get the desired result. 

 

Suppose the column in the union contains the formatted values and the original raw values are kept unformatted in a separate new column.

 

SQL Example

 

proc format;
  value $ fmt_one
    'COMPLETED' = 'Completed'
    'SPONSOR' = 'Discontinued'
  ;

  value $ fmt_two
    'COMPLETED' = 'Completed'
  ;

run;

data Table_one;
  id = 1; task = 'COMPLETED'; output;
  id = 2; task = 'SPONSOR';   output;

  format task $fmt_one.;
run;

data Table_two;
  id = 3; task = 'COMPLETED'; output;
  id = 4; task = 'SPONSOR';   output;

  format task $fmt_two.;
run;

proc sql;
* task gets format from table_one; create table table_union12 as select *, 'table_one' as source from table_one UNION select *, 'table_two' as source from table_two ;
* task gets formatted value per format and data set; create table table_union_wanted (drop=_task) as select * , put(_task,$fmt_one.) as task , _task as raw_task , 'table_one' as source from table_one (rename=task=_task) UNION select * , put(_task,$fmt_two.) as task , _task as raw_task , 'table_two' as source from table_two (rename=task=_task) ; ods html file='problem.html'; proc print data=table_one; title "Table_One with task using FMT_ONE"; proc print data=table_two; title "Table_Two with task using FMT_TWO"; proc print data=table_union12; title "Tables unioned, task is FMT_ONE"; proc print data=table_union_wanted; title "Tables unioned, task is not formatted"; run; ods html close;

Output

 

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Sorry, but i don't understand the problem you have, maybe you could add the data you have in usable form (or at least as table) so that is more obvious what you are taking about.

RichardDeVen
Barite | Level 11

You want to stack data so the formatted value is different based on which table the value came from.  However, Proc SQL will assign a column the format that is from the first table in the UNION and a column can have only one format.

 

Proc FORMAT maps a raw value to a single formatted value. (And, yes, a multi-label format (MLF) can map a single value to more than one value and some Procedures are MLF 'aware')

 

In your case MLF is not plausible, leaving you with a conundrum.

 

You will need some data transformation to get the desired result. 

 

Suppose the column in the union contains the formatted values and the original raw values are kept unformatted in a separate new column.

 

SQL Example

 

proc format;
  value $ fmt_one
    'COMPLETED' = 'Completed'
    'SPONSOR' = 'Discontinued'
  ;

  value $ fmt_two
    'COMPLETED' = 'Completed'
  ;

run;

data Table_one;
  id = 1; task = 'COMPLETED'; output;
  id = 2; task = 'SPONSOR';   output;

  format task $fmt_one.;
run;

data Table_two;
  id = 3; task = 'COMPLETED'; output;
  id = 4; task = 'SPONSOR';   output;

  format task $fmt_two.;
run;

proc sql;
* task gets format from table_one; create table table_union12 as select *, 'table_one' as source from table_one UNION select *, 'table_two' as source from table_two ;
* task gets formatted value per format and data set; create table table_union_wanted (drop=_task) as select * , put(_task,$fmt_one.) as task , _task as raw_task , 'table_one' as source from table_one (rename=task=_task) UNION select * , put(_task,$fmt_two.) as task , _task as raw_task , 'table_two' as source from table_two (rename=task=_task) ; ods html file='problem.html'; proc print data=table_one; title "Table_One with task using FMT_ONE"; proc print data=table_two; title "Table_Two with task using FMT_TWO"; proc print data=table_union12; title "Tables unioned, task is FMT_ONE"; proc print data=table_union_wanted; title "Tables unioned, task is not formatted"; run; ods html close;

Output

 
pdhokriya
Pyrite | Level 9
@RichardADeVenezia : Thank you so much for the quick reply. and help.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 2634 views
  • 0 likes
  • 3 in conversation