SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Representation of column numbers

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Representation of column numbers

Dear all,

      Today i came with another stupid question, That i am having problem with column representation and the dataset given bellow

                  

                   Untitled.png

Now I need to create another dataset with all these variables and an extra variable which is calculated from T1, T2, T3, T4, Lower. Consider T1 as 1, T2 as 2, T3 as 3, T4 as 4. nSuppose ” lower”  contains value 0.53 which is positioned at “T2” so the new variable will contains 2 and “lower “ is 1 then new variable contains 3 ……

For the above dataset the calculated result will be given in dataset bellow. Resulted variable spoted in green color.

                    Untitled1.png

Thanks in advance.


Accepted Solutions
Solution
‎07-30-2016 07:19 AM
Super User
Posts: 10,020

Re: Representation of column numbers

Posted in reply to Steelers_In_DC

The more succinct code is :

Code: Program

data have;
input t1-t4 low;
cards;
0 0.51 1 2 1
0 0.53 1 2.02 0.53
;
run;
data want;
set have;
array x{*} t:;
first=whichn(low,of x{*});
run;

View solution in original post


All Replies
Valued Guide
Posts: 860

Re: Representation of column numbers

Posted in reply to satish123

This is simple enough, I was trying to find a simpler solution using scan but didn't get there.  I'm hoping someone comes up with something more eligant in case this is a situation with a large dataset.

data have;

infile cards dsd dlm= ' ';

input c1 c2 t1 t2 t3 t4 lower;

cards;

.7514 10.215 0 .51 1 2 1

1.08674 9.123 0 .53 1 2.02 .53

1.2134 10.324 0 .5 1 2 2

.23421 12.1234 0 .5 1.03 2 1.03

;

run;

data want;

set have;

if lower = t1 then first = 1;

else if lower = t2 then first = 2;

else if lower = t3 then first = 3;

else if lower = t4 then first = 4;

run;

Super User
Posts: 10,020

Re: Representation of column numbers

Posted in reply to satish123

Code: Program

data have;
input t1-t4 low;
cards;
0 0.51 1 2 1
0 0.53 1 2.02 0.53
;
run;
data want;
set have;
array x{*} t1-t4;
do i=1 to dim(x);
  if x{i}=low then do;first=i;leave;end;
end;
drop i;
run;

Valued Guide
Posts: 860

Re: Representation of column numbers

Xia,

I think I've seen this but do not remember the solution, if you do not know how many t values there are is there a way to say array x{*} t1-t 'nth; ?


Thanks,

Solution
‎07-30-2016 07:19 AM
Super User
Posts: 10,020

Re: Representation of column numbers

Posted in reply to Steelers_In_DC

The more succinct code is :

Code: Program

data have;
input t1-t4 low;
cards;
0 0.51 1 2 1
0 0.53 1 2.02 0.53
;
run;
data want;
set have;
array x{*} t:;
first=whichn(low,of x{*});
run;
Super User
Super User
Posts: 7,942

Re: Representation of column numbers

Posted in reply to satish123

Hi,

data want;

     set have;

     array t{4};

     do i=1 to 4;

          if t{i} <= lower and first=. then first=i;

     end;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 506 views
  • 1 like
  • 4 in conversation