DATA Step, Macro, Functions and more

Loop thru values in a data set

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Loop thru values in a data set

I am an experienced developer in everthing but SAS.

 

I have a Dataset --->   "Periods" with a single column and 12 periods:

 

YYYYPP

201605

201604

201603

201602

....

...

 

I want step thru each data format in the Periods table, run an query to pull based on each of the 12 dates, by setting the current period equal to a variable and using the variable as criteria for the pull. 

 

I can do all parts of this except the looping part of the code. 

This is my approach and I can't get it to work.

 

%Macro macro1;

%Do i = 1 %To 12 %By 1;

 

call symputx ('YYYYPP',trim(YYYYPP));

 

RunQuery using &YYYYPP.

 

%end

%Mend macro1; %macro1;

 

Any Help is appreciated in advance.

 

Thank you.

 


Accepted Solutions
Solution
‎06-16-2016 03:03 PM
Super User
Posts: 5,503

Re: Loop thru values in a data set

Posted in reply to Kody_devl

You're moving in the right direction.  Add a semicolon at the end of the %END statement, and embellish on the CALL SYMPUTX line to make it a full DATA step:

 

data _null_;

set have (obs=&i firstobs=&i);

call symputx('YYYYPP', yyyypp);

run;

 

With CALL SYMPUTX, the TRIM function is not needed.  Leading and trailing blanks are automatically removed.  In fact, CALL SYMPUTX will even work properly if your incoming variable is numeric instead of character.

 

Net result:  &YYYYPP gets updated each time through the loop, and is available for your query.

View solution in original post


All Replies
Solution
‎06-16-2016 03:03 PM
Super User
Posts: 5,503

Re: Loop thru values in a data set

Posted in reply to Kody_devl

You're moving in the right direction.  Add a semicolon at the end of the %END statement, and embellish on the CALL SYMPUTX line to make it a full DATA step:

 

data _null_;

set have (obs=&i firstobs=&i);

call symputx('YYYYPP', yyyypp);

run;

 

With CALL SYMPUTX, the TRIM function is not needed.  Leading and trailing blanks are automatically removed.  In fact, CALL SYMPUTX will even work properly if your incoming variable is numeric instead of character.

 

Net result:  &YYYYPP gets updated each time through the loop, and is available for your query.

Contributor
Posts: 58

Re: Loop thru values in a data set

Posted in reply to Astounding

 

Your were correct. 

I was so close (and yet soooo far).

 

You have no idea how long I have been struggling with this simple thing.

 

THANK YOU!

 

Kody_Devl

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 231 views
  • 0 likes
  • 2 in conversation