DATA Step, Macro, Functions and more

Creating subscripted variables in loop

Reply
N/A
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?

Thanks

GT Message was edited by: Turgidson
SAS Super FREQ
Posts: 8,743

Re: Creating subscripted variables in loop

Hi:
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:
http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

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:
http://support.sas.com/documentation/cdl/en/imlug/59656/PDF/default/imlug.pdf Chapter 4 discusses how to work with IML matrices, including subscripting.

cynthia
N/A
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.

Y_i=i

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

Thanks

GT
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. ;
END;

MACRO language:

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


Suggest you start with some basic SAS system reading and go from there - links to SAS support http://support.sas.com/ 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:
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a001281588.htm

Step-by-Step Programming with Base SAS(R) Software - Introduction to DATA Step Processing
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001302699.htm

SAS Macro Language: Reference
Introduction to the Macro Facility
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293969.htm

SAS Language Reference by Name, Product, and Category - Macro Statements Syntax Sorted Alphabetically
http://support.sas.com/documentation/cdl/en/allprodslang/62304/HTML/default/syntaxByType-macroStatem...

SAS SQL Procedure User's Guide - Introduction to the SQL Procedure
http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/a002536894.htm


CONFERENCE PAPER references:

A Hands-On Introduction to SAS® DATA Step Programming
Debbie Buck, D. B. & P. Associates, Houston, TX (a SUGI conference paper)
http://www2.sas.com/proceedings/sugi30/134-30.pdf

Understanding the SAS® DATA Step and the Program Data Vector
Steven First, Systems Seminar Consultants, Madison, WI
http://support.sas.com/resources/papers/proceedings09/136-2009.pdf

SAS Macro Programming for Beginners
Susan J. Slaughter, Avocet Solutions, Davis, CA
Lora D. Delwiche, Delwiche Consulting, Winters, CA
http://www2.sas.com/proceedings/sugi29/243-29.pdf
Ask a Question
Discussion stats
  • 4 replies
  • 133 views
  • 0 likes
  • 4 in conversation