SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Automating Sequential Variable Processing

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Automating Sequential Variable Processing

How do I automate this? I mean this is so easy even in programming languages like C++, Java, and R. But in SAS, I have tried arrays, global variables, functions, and a number of other things only to not get what I need. I am just trying to cycle through a set of variables that have the same name, but with a different sequence number. How in the world is this done in SAS? ata etn.etn.table3; set etn.table2 array ayear [39] 4. ayr1973-ayr2011; if AGE = 0 then do; ayr1973 = yr1973; ayr1974 = yr1974; ayr1975 = yr1975; ayr1976 = yr1976; ayr1977 = yr1977; ayr1978 = yr1978; ayr1979 = yr1979; ayr1980 = yr1980; ayr1981 = yr1981; ayr1982 = yr1982; ayr1983 = yr1983; ayr1984 = yr1984; ayr1985 = yr1985; ayr1986 = yr1986; ayr1987 = yr1987; ayr1988 = yr1988; ayr1989 = yr1989; ayr1990 = yr1990; ayr1991 = yr1991; ayr1992 = yr1992; ayr1993 = yr1993; ayr1994 = yr1994; ayr1995 = yr1995; ayr1996 = yr1996; ayr1997 = yr1997; ayr1998 = yr1998; ayr1999 = yr1999; ayr2000 = yr2000; ayr2001 = yr2001; ayr2002 = yr2002; ayr2003 = yr2003; ayr2004 = yr2004; ayr2005 = yr2005; ayr2006 = yr2006; ayr2007 = yr2007; ayr2008 = yr2008; ayr2009 = yr2009; ayr2010 = yr2010; ayr2011 = yr2011; end; else ayr1973 = yr1973; run;

Accepted Solutions
Solution
‎11-08-2016 10:00 AM
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

Ah...that's not my code Smiley Happy

 

The loop counter is set incorrectly. 

Whrn you declare an array with (1973:2011) the array needs to be indexed with those values. 

 

Change the do line to

 

do I=1973 to 2011;

View solution in original post


All Replies
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

Definitely a use for arrays. What did you try?

Occasional Contributor
Posts: 11

Re: Automating Sequential Variable Processing

I tried a number of things, so many I forget because I erased them. But my main problem was getting the variables to recognized as variables.

Also, I don't know what it is about my Browser settings, but it is difficult to use this forum because everything is centered and I am writing in a tiny text box in which I can't really see what I am typing.
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

Use anything other than IE/Edge and hit Reply not quick reply. 

The only issue I could see possibly running into is if all variables aren't same type. 

 

Array  varin (*) var1-var25;

array varout(*) vout1-vout25;

 

If (condition) then do;

    Do I=1 to dim(varin);

     Varout(I)=varin(I);

    End;

end;

 

Array 101 tutorial

http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/

Occasional Contributor
Posts: 11

Re: Automating Sequential Variable Processing

The statements in the if bloc need to be transferring values from one variable to another, which just using arrays does not accomplish. I tried the above code before and I got empty cell values. My main objective is to cycle through one variable name var1 to varN, transferring the values in each variable to the corresponding newVar1 to newVarN.
Super User
Posts: 9,676

Re: Automating Sequential Variable Processing

Yeah. Post some data and output to explain your question.




  data table3; 
  set table2 ;
  array year [1973:2011]  yr1973-yr2011;
  array ayear [1973:2011]  ayr1973-ayr2011;
   if AGE = 0 then do; 
     do i=1 to dim(year);
      ayear[i]=year[i];
     end;
   end;
   else ayr1973 = yr1973; 
  run;
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

@Ksharp Does I=1 work as an index in your code?

Super User
Posts: 9,676

Re: Automating Sequential Variable Processing

Opps. Should use LBOUND() and HBOUND() .
Occasional Contributor
Posts: 11

Re: Automating Sequential Variable Processing

The statements in the if bloc need to be transferring values from one variable to another, which just using arrays does not accomplish. I tried the above code before and I got empty cell values. My main objective is to cycle through one variable name var1 to varN, transferring the values in each variable to the corresponding newVar1 to newVarN.

For example, in the first iteration the value in yr1973 needs to be transferred into ayr1973. Then for the second iteration the value of yr1974 needs to be transferred into ayr1974... the process continues until 2011.

I cannot get the code to recognize yr1973 and ayr1973 as variables.
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

The code works   Please try it and post any errors or explain any issues with sample data. 

Occasional Contributor
Posts: 11

Re: Automating Sequential Variable Processing

Yes, the code works, technically it throws no errors. However, it does not transfer the values from the old variable to the new variable. All the cells are empty. There is no error message to post.
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

Post your exact code please. 

Occasional Contributor
Posts: 11

Re: Automating Sequential Variable Processing

data etn.table3;
set etn.table2;
by BEN_AGE;

array year [1973:2011] yr1973-yr2011;
array ayear [1973:2011] ayr1973-ayr2011;

if AGE = 0 then do;
do i = 1 to dim(year);
ayear[i]= year[i];

end;
end;
else
ayr1973 = yr1973;

run;
Solution
‎11-08-2016 10:00 AM
Super User
Posts: 17,819

Re: Automating Sequential Variable Processing

Ah...that's not my code Smiley Happy

 

The loop counter is set incorrectly. 

Whrn you declare an array with (1973:2011) the array needs to be indexed with those values. 

 

Change the do line to

 

do I=1973 to 2011;

Super User
Posts: 10,500

Re: Automating Sequential Variable Processing


RedMcCallen wrote:
data etn.table3;
set etn.table2;
by BEN_AGE;

array year [1973:2011] yr1973-yr2011;
array ayear [1973:2011] ayr1973-ayr2011;

if AGE = 0 then do;
do i = 1 to dim(year);
ayear[i]= year[i];

end;
end;
else
ayr1973 = yr1973;

run;

That code should have been generating array subscript out of range error messages.

☑ This topic is SOLVED.

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

Discussion stats
  • 14 replies
  • 411 views
  • 0 likes
  • 4 in conversation