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

Hi All, 

 

The following SAS code creates a dataset list that contains the variable snapshot (in yyyymm format) from 200309 to 201603. There is a "if" condition that is supposed to result in only the snapshots 200309 to 201506 being populated. However, the "if" condition does not work. Please advise on why this is so. Any help would be much appreciated. Thanks!

 

%let   strte=200309;
%let   ende=201506;

%macro download;

data list(drop=i);
	do i=0 to 150;
		index=i;
		snapshot=put(intnx('month',input("&strte.",yymmn6.),i),yymmn6.);
		output;
	end;
	if snapshot<="&ende.";
run;


%mend download;


%download;
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You must make the OUTPUT statement conditional

 

%let   strte=200309;
%let   ende=201506;

%macro download;

data list(drop=i);
	do i=0 to 150;
		index=i;
		snapshot=put(intnx('month',input("&strte.",yymmn6.),i),yymmn6.);
		if snapshot<="&ende." then output;
	        end;
run;


%mend download;


%download;

OUTPUT is an executable statement. You can't cancel its effect with later statements. 

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

You must make the OUTPUT statement conditional

 

%let   strte=200309;
%let   ende=201506;

%macro download;

data list(drop=i);
	do i=0 to 150;
		index=i;
		snapshot=put(intnx('month',input("&strte.",yymmn6.),i),yymmn6.);
		if snapshot<="&ende." then output;
	        end;
run;


%mend download;


%download;

OUTPUT is an executable statement. You can't cancel its effect with later statements. 

PG
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why is this in a macro, there is no need for it.  You can also write your code so that the do loop executes only for the number of intervals between the two items (note, if you avoided putting data in macro variables your life would also be much easier).  So your code can actually be written as:

%let   strte=200309;
%let   ende=201506;
data list(drop=i);
  do i=0 to intck('month',input("&strte.",yymmn6.),input("&ende",yymmn6.));
    index=i;
    snapshot=put(intnx('month',input("&strte.",yymmn6.),i),yymmn6.);
    output;
  end;
run;

This will then only loop for the needed number of iterations and outputs each time, whereas the do with and if statement will run 150 times regardless of how many times it needs to.

ballardw
Super User

And anothe approach with DO Until:

 

data list;
   snapshot = input("&strte.",yymmn6.);
   do until (snapshot > input("&ende",yymmn6.));
      output;
      snapshot= intnx('month',snapshot,1);
   end;
   format snapshot yymmn6.;
run;

Exercise for the interested reader to modify to  a DO WHILE loop.

 

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