BookmarkSubscribeRSS Feed
Sas_user987
Fluorite | Level 6

Hi,

I need to stack datasets on top of each other

 

 

data output;

set dataset1 dataset2 dataset3 dataset4;

run;

 

 

All these dataset have same column names, but because they are coming from different sources, some of them are numeric where it could be a character in the other one.

 

How can I tell SAS to stack dataset together but tat the same time convert all variables into character?

 

Thanks,

Justinas

3 REPLIES 3
RichardDeVen
Barite | Level 11

There is no built in feature that will stack variables of same name but different types.

 

The template for creating a proper target character variable in the stacked result set is to ensure the width (variable length) is wide enough and the numeric variables are transformed with a PUT using the appropriate FORMAT or some other mechanism.


Example:

 

Use VVALUE function to retrieve the formatted value of a numeric variable.

 

data want;
  length x $30;
  set 
    source_1 (in=_1)
    source_2 (in=_2 rename=(x=x_num2))
    source_3 (in=_3 rename=(x=x_num3))
  ;

  select;
    when (_1) ; 
    when (_2) x = vvalue(x_num2);
    when (_3) x = vvalue(x_num3);
    otherwise;
  end;

  drop x_num2 x_num3;
run;

Once you understand the coding pattern you can use meta data to compute the source code needed into macro variable(s) that would then be resolved in a skeleton or template DATA step that relies on the macro system resolving the computed source code(s).

 

mkeintz
PROC Star

Are these ID numbers, or other values that will never be subject to mathematical operations (average, sum, ... )?   And even if they are ID numbers, consider writing them to character values with leading zeroes, if necessary.  Otherwise a different sort order would be produced (i.e. '54' would be greater than '122', but '054' would not).

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

Define "different sources".

If you get your data from separate (business) entities in different formats (e.g. xlsx vs. text), then you should streamline your orocess by setting up agreements with a unified format for data transfer files.

If it's just multiple files in the same format, then the use of PROC IMPORT is the most likely culprit.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1211 views
  • 0 likes
  • 4 in conversation