BookmarkSubscribeRSS Feed
irparedesr
Calcite | Level 5

Hello everybody,

 

I'm new to programming in SAS, I ever use R and Python. In these language I can applied a LOOP for move through rows of column in data frame and reference a column in data frame so: "df$variable[1]" or "df.variable [1]". I want to do the same in SAS to generate a new table.

 

Could someone give me an idea? Please

 

Sorry about my grammar, I'm not very good at writing in English

 

 

 

 

 

 

 

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

Sorry, your goal is unclear. What do you do with that referenced column? What are you trying to achieve?

SASKiwi
PROC Star

Welcome to the SAS Community.

 

As a new SAS user, I think it would be well worth your while to check out some of the training videos available here: https://communities.sas.com/t5/New-SAS-User/bd-p/new-users 

Reeza
Super User

SAS does not work in a similar fashion as R or Python. It processes data line by line instead so each data step is an implicit loop by default. This is historically advantageous as it didn't matter how big your data set was, it simply went through it line by line. In comparison, R/Python typically default to in memory operations, which require loading ALL your data into memory.

 

I would recommend taking the free SAS e-courses if you're trying to learn SAS. There's also a variation on Coursera that you could use.

 

Arrays are one usage of loops but likely beyond you at the moment.

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

To create a data set you can just type out the data usually, unless you have a complicated structure?

 

 

data demo;
input ID $ var1 - var4;
cards;
A 1 2 3 4
B 5 6 7 8
C 8 9 10 11
;;;;
run;

 

 

 


@irparedesr wrote:

Hello everybody,

 

I'm new to programming in SAS, I ever use R and Python. In these language I can applied a LOOP for move through rows of column in data frame and reference a column in data frame so: "df$variable[1]" or "df.variable [1]". I want to do the same in SAS to generate a new table.

 

Could someone give me an idea? Please

 

Sorry about my grammar, I'm not very good at writing in English

 

 

 

 

 

 

 


 

ballardw
Super User

A group of variables, of the same type, would be called an ARRAY. Arrays a basically only available in the data step and the definition does not persist after the data step executes. The array only operates on one observation (record or row) at a time.

 

In a data step you define an array using the Array statement and options.

basic definition of an array for numeric values looks like:

array somename (*) var1 var2 var3 ;

If the variable already exists it will be used, and if it doesn't then a new variable with the name is created.

If creating new variables of character type you need to provide a $ to indicate the variables are character and a length:

array something (*) $ 15 var10 var11 var12;

You can make new variables with a numeric suffix like this. Character variables would again require the $ and length.

array other (3);

Would create three variables named other1 other2 and other3.

 

The easiest way to use the values in an array is an iterated loop such as

do i = 1 to dim(other);
    other[i] = rand('uniform');
end;

You use the index, the loop counter, to reference the elements of the array as they appear on the ARRAY statement. You can use ( ) instead of [ ] to enclose the index indicator but I find that in long lines of code it makes it easier to keep track of the index with the square brackets. The index value has to be in the range defined by the array statement. The index values must be sequential.

Advanced topics: Indices that start with a value other than 1, 2 or more dimension arrays.

 

There are quite a few functions that will use the whole value of the array by using the syntax (of arrayname(*) ) so that you do not have to actually loop over all the values explicitly.

The data step statistics functions like Sum, Mean, N, Nmiss and so on will use this syntax:

Total = sum(of arrayname(*) )

Many of the functions that accept varying length of variable or value lists will do this.

 

You can also use functions or operations that return 1) integer values 2) in the range of the index to reference elements of the array.

value = other [ i + 1];

for example.

 

If this is not sufficient to get you started please provide some example data of what you have in the form of a data step and what the result should look like. Example:

data have;
    input x y z;
datalines;
1 2 3
1 3 17
2 2 12
;

Please paste any data step into a code box opened on the forum by using the </> icon. This is important because this forum otherwise will reformat text and may do so in a way that will prevent the data step from running. Please test your data step code before posting to make sure that it runs correctly.

If you actual problem involves many variables please use fewer to demonstrate the issue.

 

Ksharp
Super User

Then you need to learn SAS/IML . Calling @Rick_SAS 

 

Or if it is simple operation on columns,try PROC SQL

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1258 views
  • 0 likes
  • 6 in conversation