BookmarkSubscribeRSS Feed
tinghlin
Fluorite | Level 6

I have an excel files with 3 work sheets  for year 106, 107 and 108. each year has 100 observations with the same varoables. I wrtoe an macro, but WORK.a has only 100 observations. how do I merge the 300 obsevations together into work.a ?

 

%macro pim(sheet);
proc import out= WORK.a
datafile = 'G:\data\106_108.xlsx'
DBMS=EXCEL replace;
sheet = "&sheet";
GETNAMES=YES;
run;
%mend pim;
%pim(106);
%pim(107);
%pim(108);

proc print data= WORK.a ;
run;

2 REPLIES 2
andreas_lds
Jade | Level 19

See Maxim 2: The log shows that each call of your macro creates/overwrites the same dataset. So you have to write each sheet to a different dataset first, than append them into a single dataset. Please note, that the last task may sound easy, but can be challenging, because excel doesn't have proper variable-types, forcing proc import to guess the types and set the length of alphanumeric variables depending on the values found in the data, most likely resulting in different lengths for a variable that seems to identical in all sheets.

Shmuel
Garnet | Level 18

As your excel file is of xlsx type, you can try next code:

libname myxlsx xlsx "G:\data\106_108.xlsx";

data want;
  set myxlsx.'106'n
      myxlsx.'107'n
      myxlsx.'108'n
  ;
run;

it will be better if sheet names will be renamed into "s016", "s107", "s108"

then the code can be adapted to:

libname myxlsx xlsx "G:\data\106_108.xlsx";

data want;
  set myxlsx.s106
      myxlsx.s107
      myxlsx.s108
  ;
run;

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
  • 2 replies
  • 1410 views
  • 1 like
  • 3 in conversation