DATA Step, Macro, Functions and more

Creating subscripted variables in loop

Posts: 0

Creating subscripted variables in loop

I can program this fairly well in Stata, so I am guessing that SAS and IML can do the same thing. I don't know enough about the SAS language to try and look up the bits and pieces of this in the documentation.

Here's the code in Stata:

forvalues i=0/4 {
gen fakevar_`i'=.

The internal variable i will take on values 0-4, and this will generate 5 variables with missing values: fakevar_0...fakevar_4. Stata will also interpret the `i' as the value within a text string.

How do I do this in SAS? What do you call those internal variables in loops, and how do I get the statements in the loop to use that value of i in the way I want to use it?


GT Message was edited by: Turgidson
Posts: 8,743

Re: Creating subscripted variables in loop

If you're talking about DATA step programs then SAS can treat those numbered variables as though they are members of an ARRAY. This paper has a good introduction to how ARRAYS work in SAS:

Since IML works with matrices, if your real interest is in IML, matrix addressing is different than array addressing. Here's the IML user's guide: Chapter 4 discusses how to work with IML matrices, including subscripting.

Posts: 0

Re: Creating subscripted variables in loop

I am hacking my way through IML. and all I wanted to do was create a series of variables that were subscripted.


I'm translating the code out of Stata and Matlab, where this seems to be an easier thing to do.


Valued Guide
Posts: 2,175

Re: Creating subscripted variables in loop

in a data step you can define subscripted variables in many ways. For example
length num1-num25 4;
defines 25 short numeric vars.
retain chr1 - chr9 "ab" ;
defines 9 chrN vars all stsrting with value ;ab
Super Contributor
Super Contributor
Posts: 3,174

Re: Creating subscripted variables in loop

FWIW, a separate SAS discussion forum exists for the SAS/IML product. For what you have described in your post (to some extent), there are two different SAS programming techniques, each with their own purpose to do_over/iterate - mentioned below:

DATA step language:

DO I=0 TO 4;
* your SAS DATA step code goes here. ;

MACRO language:

%DO I=0 %TO 4;
%* your SAS macro code goes here. ;

Suggest you start with some basic SAS system reading and go from there - links to SAS support website documentation and technical / conference paper references below.

Scott Barry
SBBWorks, Inc.

SAS-hosted DOCUMENTATION references:

SAS(R) 9.2 Language Reference: Concepts, DATA Step Processing:

Step-by-Step Programming with Base SAS(R) Software - Introduction to DATA Step Processing

SAS Macro Language: Reference
Introduction to the Macro Facility

SAS Language Reference by Name, Product, and Category - Macro Statements Syntax Sorted Alphabetically

SAS SQL Procedure User's Guide - Introduction to the SQL Procedure


A Hands-On Introduction to SAS® DATA Step Programming
Debbie Buck, D. B. & P. Associates, Houston, TX (a SUGI conference paper)

Understanding the SAS® DATA Step and the Program Data Vector
Steven First, Systems Seminar Consultants, Madison, WI

SAS Macro Programming for Beginners
Susan J. Slaughter, Avocet Solutions, Davis, CA
Lora D. Delwiche, Delwiche Consulting, Winters, CA
Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation