DATA Step, Macro, Functions and more

Hoiw to get variable from values in SAS?

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Hoiw to get variable from values in SAS?

[ Edited ]

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?

 

 


Accepted Solutions
Solution
‎03-30-2017 05:07 AM
New Contributor
Posts: 4

Re: Hoiw to get variable from values in SAS?

Thanks Sir..

I got solution, This code is working fine.Smiley Happy

 

View solution in original post


All Replies
PROC Star
Posts: 7,487

Re: Hoiw to get variable from values in SAS?

Posted in reply to contact2anil
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

New Contributor
Posts: 4

Re: Hoiw to get variable from values in SAS?

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

Respected Advisor
Posts: 4,173

Re: Hoiw to get variable from values in SAS?

[ Edited ]
Posted in reply to contact2anil

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

 

Which SAS version are you using?

New Contributor
Posts: 4

Re: Hoiw to get variable from values in SAS?

Ok Thanks Patrick. It may be.
I am using SAS 9.0.
Respected Advisor
Posts: 4,173

Re: Hoiw to get variable from values in SAS?

Posted in reply to contact2anil

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.

 

 

PROC Star
Posts: 7,487

Re: Hoiw to get variable from values in SAS?

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

Solution
‎03-30-2017 05:07 AM
New Contributor
Posts: 4

Re: Hoiw to get variable from values in SAS?

Thanks Sir..

I got solution, This code is working fine.Smiley Happy

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 167 views
  • 5 likes
  • 3 in conversation