BookmarkSubscribeRSS Feed
Kavindra
Calcite | Level 5

Hi everyone,

A very good day!

Let us say I have a dataset:

Name| Var1| Var2 | Var3

A      |23     |25     |28

B      |45     |33     |32

C      |23     |65     |67

D      |13     |14     |14

E      |65     |66     |69

Where Name is character and Var1-3 are numeric.

Problem:

If the name is equal to A or B, all the numeric values should be multiplied by 100.

Is it possible to do it without looping or arrays. Something of the nature,

If Name IN ("A" "B") then do;

_numeric_ *100;

RUN;

This is just a run of the mill thought and doesn't mean it is right!

                   

Any other solutions welcome.

Regards,

Kavindra

7 REPLIES 7
Kavindra
Calcite | Level 5

Actually the variables are named differently and using Var1-3 is not possible!

Let us say,

dollars1-24, units1-24 and packet1-24

jakarman
Barite | Level 11

What is the reason for asking without looping or arrays?

It are programming elements that should make coding easier.

Looping over records is done automatic. SAS is doing that SQL is doing that.

As it is already happening you do not need to code by yourself.

Arrays are a feature to do a lot of the same without typing it all yourself. SAS(R) 9.4 Language Reference: Concepts

There are many automatic ways working with arays or naming conventions (* :   Variable lists)   

---->-- ja karman --<-----
Kavindra
Calcite | Level 5

Basically the numeric variables are named very diversely. So, there is no way to rename them without tediously writing down their names. So, it would be helpful if it could be done for all numeric variables.

Vince28_Statcan
Quartz | Level 8

Hi Kavindra,

You can use _NUMERIC_ to define an array referencing all numeric variables in the data vector and then loop on that array.

data want;

     set have;

     array temp {*} _NUMERIC_;

     if name in ("A", "B") then do;

          do i=1 to dim(temp);

               temp{i}=temp{i}*100;

          end;

     end;

     drop i;

run;

It's not quite "without arrays" but it read to me as though your issue was that of getting all of your numeric variables in an array when not knowing all the variables names or, well, not wanting to hard type them all because they are not ranges. I have to agree with Jaap in that you shouldn't restrain yourself not to use arrays. They often provide the easiest solution and figuring out the appropriate syntax to solve your loop issue is what the SAS community/documentation is there for.

However, as a possible alternative to obtaining all of your numeric variables names, you can always consider using SASHELP.vcolumn automatic table. Either load the name of the numeric variables in a macro for a proc datasets rename or just to use these macros to build the list of variables for an array statement for example.

Vince

Eh apparently I got beat to it by Rick while writting up Smiley Happy sorry for duplicate code.

jakarman
Barite | Level 11

This looks a nice reference using the _numeric_ list.  http://www2.sas.com/proceedings/sugi30/242-30.pdf

Do not be afraid of array-s. See the allnums usage sample with the dimension automatic supplies.

Some surprise, the 24 number should be a coincidence, possible a common 2 year interval approach.  

---->-- ja karman --<-----
Rick_SAS
SAS Super FREQ

data Have;
input Name $ Var1 Var2 Var3;
datalines;
A      23     25     28
B      45     33     32
C      23     65     67
D      13     14     14
E      65     66     69
;

data Want(drop i);
   set have;
   array x{*} _numeric_;   /* all numeric vars */
   if Name IN ("A" "B") then do;
      do i=1 to dim(x);
         x{i} = 100*x{i};
      end;
   end;
run;

jakarman
Barite | Level 11

thx Rick Vince,

I was just pulling at Kavindra's mind/brains for the why's and how's the requirements and options with questions and giving some doc's..

---->-- ja karman --<-----

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

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 4647 views
  • 0 likes
  • 4 in conversation