Learning SAS? Welcome to the exclusive online community for all SAS learners.

Forval equivalent in SAS

Reply
Occasional Learner NLI
Occasional Learner
Posts: 1

Forval equivalent in SAS

I want to create a date variable that increases at monthly increment (given a start and end range), then this date variable is compared to a time range in the dataset and will be recorded as a value 1 or 0. 

 

I struggle to find the equivalent of Forval in STATA in SAS

forval year=2006/2016 {

     forval month=1/12 {

            gen var _'year'_'month' = 1 if start_date <=date("'month'-01-'year'",'MDY") <= end_date

                   }

            }

 

How do I translate this into SAS do loops? 

 

Any insight would be appreciated. 

 

 

Super User
Posts: 17,868

Re: Forval equivalent in SAS

It's probably easier if you post sample data illustrating what you have and what you want. 

As is, you'll need to wait for someone who understands both Stata and SAS to answer your question and it will take longer. 

 

Some things that will probably be useful

 

INTNX() function ->used to increment date variables, can be via a day, month, year or other custom interval.

MDY() -> calculate a date

Dates are stored as numbers, as the number of days from Jan 1, 1960, and can be subtracted to determine daily differences.

 

Here's a simple program to create 24 monthly dates in a data set. I don't think this relates to what you want, just illustrates how to loop and increment date variables. 

 

data date_loop;
start_date = '01Jan2016'd;

do i=1 to 24;
   date=intnx('month', start_date, i-1, 'b');
   output;
end;

run;

Ask a Question
Discussion stats
  • 1 reply
  • 133 views
  • 0 likes
  • 2 in conversation