BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
contact2anil
Fluorite | Level 6

Suppose we hv below data set..
I want to get max month from each obersvations in new variable.
like from 1st obs Mar and in 2nd obs Apr..

 

data year;
input jan feb mar apr jun;
datalines;
100 234 340 111 231
111 212 213 314 123
run;

 

By using below code we can calcuate maximum value

data aaa;
set year;
top=max(jan,feb,mar,apr,jun);
run;

 

Output:
 Obs    jan    feb    mar    apr    jun    top
 1     100    234    340    111    231    340
 2     111    212    213    314    123    314

 

But i want to find variable which max value??

Please suugest solution?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
contact2anil
Fluorite | Level 6

Thanks Sir..

I got solution, This code is working fine.:)

 

View solution in original post

7 REPLIES 7
art297
Opal | Level 21
data want;
  input jan feb mar apr jun;
  array list(*) jan--jun;
  top=max(of list(*));
  topvar = vname(list[whichn(max(of list[*]), of list[*])]);
  datalines;
100 234 340 111 231
111 212 213 314 123
;

Art, CEO, AnalystFinder.com

contact2anil
Fluorite | Level 6

Hi Sir,

I am getting below error .. please check.

 

91   data want;
92     input jan feb mar apr jun;
93     array list(*) jan--jun ;
94     top=max(of list(*));
95     topvar = vname(list[whichn(max(of list[*]), of list[*])]);
                           ------
                           68
ERROR 68-185: The function WHICHN is unknown, or cannot be accessed.

96     datalines;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0
         observations and 7 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Patrick
Opal | Level 21

You're obviously using an old SAS version where function WHICHN() hasn't been implemented yet.

 

Which SAS version are you using?

contact2anil
Fluorite | Level 6
Ok Thanks Patrick. It may be.
I am using SAS 9.0.
Patrick
Opal | Level 21

I'm not aware of a commercial SAS 9.0 version. Here are the commercial versions: http://support.sas.com/techsup/support.html 

 

In case you're using SAS "at home" for educational purposes: Download the free of charge non-commercial version and things will work in a modern environment. https://www.sas.com/en_au/software/university-edition.html 

 

Please execute:

proc setinit; 
run;

Then consult the SAS log and tell us which SAS version you see there.

 

 

art297
Opal | Level 21

Since you don't have the whichn function, you can simply change that code to use a do loop to identify the corrrect variable:

 

data want;
  input jan feb mar apr jun;
  array list(*) jan--jun;
  top=max(of list(*));
/*  topvar = vname(list[whichn(max(of list[*]), of list[*])]);*/
  do i=1 to dim(list);
    if list(i) eq max(of list[*]) then do;
      topvar=vname(list[i]);
      return;
    end;
  end;
  datalines;
100 234 340 111 231
111 212 213 314 123
;

Art, CEO, AnalystFinder.com

contact2anil
Fluorite | Level 6

Thanks Sir..

I got solution, This code is working fine.:)

 

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
  • 7 replies
  • 1129 views
  • 5 likes
  • 3 in conversation