BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RedMcCallen
Calcite | Level 5
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;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Ah...that's not my code 🙂

 

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

14 REPLIES 14
Reeza
Super User

Definitely a use for arrays. What did you try?

RedMcCallen
Calcite | Level 5
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.
Reeza
Super User

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/

RedMcCallen
Calcite | Level 5
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.
Ksharp
Super User
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;
Reeza
Super User

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

Ksharp
Super User
Opps. Should use LBOUND() and HBOUND() .
RedMcCallen
Calcite | Level 5
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.
Reeza
Super User

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

RedMcCallen
Calcite | Level 5
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.
Reeza
Super User

Post your exact code please. 

RedMcCallen
Calcite | Level 5
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;
Reeza
Super User

Ah...that's not my code 🙂

 

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;

ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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