BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

Using SAS EG 7.1.

 

Basically for &LASTYEAR, the dataset with MONTHF =12 may not exist. So is there a way to ensure only the existing datasets are merged? Thank you

 


%let monlist = 06 12;

data Merged;
  set
  %do YEAR=&FIRSTYR %TO &LASTYR;
	%do i = 1 %to %sysfunc(countw(&monlist.));
    	%let monthf = %sysfunc(putn(%scan(&monlist.,&i.),z2.));
         Merged&YEAR.&MONTHF
  	%end; 
  %end;

run;
3 REPLIES 3
SASKiwi
PROC Star
%let monlist = 06 12;

data Merged;
  set
  %do YEAR=&FIRSTYR %TO &LASTYR;
	%do i = 1 %to %sysfunc(countw(&monlist.));
    	%let monthf = %sysfunc(putn(%scan(&monlist.,&i.),z2.));
        %if %sysfunc(exist(Merged&YEAR.&MONTHF.)) %then %do;
           Merged&YEAR.&MONTHF
          %end;
  	%end; 
  %end;

run;

Corrected thanks to @FreelanceReinh

Reeza
Super User

You're appending (stacking datasets) not merging. 

 

If you have the option, consider using a variable list or shortcut list instead of a macro to list the datasets.

 

For example you can can use the following to append all datasets that start with MONTH.

 

Set MONTH:;

Or you can use the following to append a series with each year and then anything that starts with Month2007, Month2008 and Month2009 will be included. 

 

Set Month2007: Month2008: Month2009:;

 

FreelanceReinh
Jade | Level 19

@apple: The SET statement needs a closing semicolon after the second %END statement.

@SASKiwi: The argument of EXIST should be Merged&YEAR.&MONTHF. Interestingly, if none of the datasets existed, the resulting "empty" SET; statement would be interpreted as SET _LAST_; and thus possibly read some random dataset.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 955 views
  • 2 likes
  • 4 in conversation