BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

Hello friends, please help me to understand this code, this is not my code, its existing code 

 

I am testing code where one dataset "test" resolving with 3 observation 

 

date            file_name 

20Jan16     test_file_20160120.txt

19Jan16     test_file_20160119.txt

18Jan16     test_file_20160118.txt

 

Now after this dataset (test) created, below code is running and its giving only 1 observation, i really don't know why it is resolving to one but i am expecting it should has 3 observation 

 

 

%macro test (x_date);

%let store_x_date="null";

data test_2;

retain stop_ind;
if _N_ = 1 then stop_ind = 0;
set test;
by DESCENDING DATE;
if last.DATE = 1 then do;
if stop_ind = 0 then do;
output;
Author_DATE = put(DATE, date.);
call symput("&x_date.",Author_DATE);
end;
stop_ind = 1;
end;
if stop_ind = 0 then output;
run;

%mend;

%test (store_x_date);

 

 

Thank you

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Some contributing factors:

 

  • Since the DATA step contains an OUTPUT statement, you will only output an observation when an OUTPUT statement executes.
  • All OUTPUT statements depend on having STOP_IND=0
  • For this particular set of 3 observations, the 1st observation will output and then set STOP_IND=1
  • For the 2nd and 2rd observations, STOP_IND=1, so nothing gets output

I hope this is enough ... there's really no substitute for understanding what the statements do.

View solution in original post

3 REPLIES 3
Reeza
Super User

This is where formatting your code helps.  Note that there's an output statement nested in an IF condition and a second one in the last statement. The goal seems to be to create a macro variable.

 

%macro test (x_date);

%let store_x_date="null";

  data test_2;
  retain stop_ind;
  
  if _N_ = 1 then stop_ind = 0;

  set test;
  by DESCENDING DATE;

  if last.DATE = 1 then do;
        if stop_ind = 0 then do;
              output;
              Author_DATE = put(DATE, date.);
              call symput("&x_date.",Author_DATE);
        end;
   stop_ind = 1;
   end;

   if stop_ind = 0 then output;
   run;
%mend;

%test (store_x_date);
Astounding
PROC Star

Some contributing factors:

 

  • Since the DATA step contains an OUTPUT statement, you will only output an observation when an OUTPUT statement executes.
  • All OUTPUT statements depend on having STOP_IND=0
  • For this particular set of 3 observations, the 1st observation will output and then set STOP_IND=1
  • For the 2nd and 2rd observations, STOP_IND=1, so nothing gets output

I hope this is enough ... there's really no substitute for understanding what the statements do.

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

Thanks a lot Astouning and Reeza, appreciate your input, 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 837 views
  • 1 like
  • 3 in conversation