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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

5 REPLIES 5
Steelers_In_DC
Barite | Level 11

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;

Ksharp
Super User

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;

Steelers_In_DC
Barite | Level 11

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,

Ksharp
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

data want;

     set have;

     array t{4};

     do i=1 to 4;

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

     end;

run;

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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