BookmarkSubscribeRSS Feed
satya_majhi
Fluorite | Level 6

Hi All, I have come across this - 

 

JanFebMarchApr
10020030060
100200 40
100  20

 

I want to give the discount of the last month sales , I want the last column ( Apr) with this value 

4 REPLIES 4
stat_sas
Ammonite | Level 13

Hi,

 

Not sure, seems like you are trying to create a variable based on 20% of last populated variable value. Please try this.

 

data want(drop=i);
set have;
array m(*) _numeric_;
do i=1 to dim(m);
   if m(i)>. then Apr=0.2*m(i);
end;
run;

satya_majhi
Fluorite | Level 6

I imported the data and it seems like this,

 

image.png

 

Then I use the code and I got 

 

image.png

stat_sas
Ammonite | Level 13

Does this work with your initial data set?

ballardw
Super User

Pictures are not data and your example input is likely incomplete in some form or another.

If you have any other numeric variable in the data set then the array _numeric_ will not give you the result you want. Please see this example:

data have;
   input m1 m2 m3 other;
datalines;
100 200 300 1
100 200 .   2
100 .   .   3
run;

data want(drop=i);
set have;
array m(*) _numeric_;
do i=1 to dim(m);
   if m(i)>. then Apr=0.2*m(i);
end;
run;

If you have any other numeric variables in the data set then the array has to either explicitly name the variable or have a naming convention that will allow a handy list short cut.

 

See:

data want(drop=i);
set have;
array m(*) m1 m2 m3;
do i=1 to dim(m);
   if m(i)>. then Apr=0.2*m(i);
end;
run;
/* which could be because the variable names are "nice"*/
data want(drop=i);
set have;
array m(*) m:; /* or m1-m3*/
do i=1 to dim(m);
   if m(i)>. then Apr=0.2*m(i);
end;
run;

However there will be problems with this process as it appears that you add a column each month and the code for 1) the array variable list would change and 2) the name of the result variable (Apr) would change. Solutions dealing with that can range from ugly to very fragile depending on the whole process involved.

 

For extra added fun figure out what happened with the value of Apr from this code:

data have;
   input m1 m2 m3 ;
datalines;
100 200 300 1
100 200 .   2
100 .   .   3
run;

data want(drop=i);
   set have;
   label apr='Discount month';
   array m(*) _numeric_;
   do i=1 to dim(m);
      if m(i)>. then Apr=0.2*m(i);
   end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1132 views
  • 0 likes
  • 3 in conversation