BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to merge data sets only if they exists (to prevent error).

Why this code is not working please?

May anyone show the way to solve the problem?

 

Data t01042021;
input Id x;
cards;
1 10
2 15
;
run;

Data t03042021;
input Id x;
cards;
3 14
2 16
;
run;

Data t07042021;
input Id x;
cards;
4 17
1 28
;
run;


%let list=t01042021+t02042021+t03042021+t04042021+t05042021+t06042021+t07042021;
%let n=7;
Data customers; input ID ; cards; 1 2 ; Run; %macro ttt; %do j=1 %to &n.; %let YYYYMMDD=%scan(&List.,&j.,+); %if %sysfunc(exist(t&YYYYMMDD.)) %then %do; proc sort data=t&YYYYMMDD.;by ID;Run; proc sort data=customers ;by ID;Run; Data new&YYYYMMDD.; merge t&YYYYMMDD.(in=a)customers(in=b); by ID; IF a and b; Run; %end; %end; %mend ttt; %ttt;
1 REPLY 1
FreelanceReinh
Jade | Level 19

Hello @Ronein,

 

The values of macro variable YYYYMMDD start with "t" so that your references t&YYYYMMDD. resolve to names starting with "tt" and no such datasets exist. So, either exclude the "t" from YYYYMMDD

%let YYYYMMDD=%substr(%scan(&List.,&j.,+),2);

(note that this has an impact on the new dataset name new&YYYYMMDD.) or simply remove the prefix "t" where it precedes macro variable references &YYYYMMDD. (in three places in your code).

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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