BookmarkSubscribeRSS Feed
Garik
Obsidian | Level 7

Hi, I have a question. How can I write a code in order to loop horizontally.

 

I must sum the values of the X1 - X10   {S = sum(of X1- X10)} but depending on their value.

 

For instance, X{i} is omitted  if it is less than 5.

or,

if the column name equals "Class_A" then the appropriate value of the column must be added to the sum.

Thanks. 

6 REPLIES 6
error_prone
Barite | Level 11
You will want to look up the syntax of array, iterative do-loop and the if-statement.

Post the code you have.
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want;
  set have;
  array x{10};
  do i=1 to 10;
    s=s+ifn(x{i}>5,x{i},0);
  end;
run;
ChrisBrooks
Ammonite | Level 13

Whenever I see someone doing/wanting to do a horizontal loop I immediately say to myself "that's spreadsheet thinking". In the long run you're much better off transposing your variables so you have a long and thin data set. For example what if, next time you want to run your code you have 15 variables instead of 10? You'll have to edit your code and that will become tedious and error-prone over time - much better to transpose it and use by-group processing which will remove that issue.

Garik
Obsidian | Level 7

Hi. if the dataset contains 20 mln rows and 245 columns! would the transposing work?

Kurt_Bremser
Super User

Transposing from wide to long is no problem, as SAS only needs to treat one record at a time. And if you can set a rule for values that can be dropped (ie missing or 0), you might even end up with a physically smaller dataset.

ChrisBrooks
Ammonite | Level 13

As @Kurt_Bremser says a long data set will (in my experience) usually be more efficient than a wide one even with more rows. Having said that if you were my client I'd be looking very hard at this data - with 20 million rows and 250 variables there's probably room for some reorganisation which would make processing more efficient and save on space.

 

I've been doing this sort of thing professionally for 30 years and turned numerous clients data around from wide to long - they're usually sceptical at first but once they see the transposed data sets in action they're quickly turned around 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 6 replies
  • 1825 views
  • 6 likes
  • 5 in conversation