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

Hi Everyone,


I have the following data with Window and code1 code2... code100 variables.

For a given record, I compare the value in Window variable with the numerical part (the last 1 or 2 character) of the name of code. If they are the same the new Value variable will be equal to the value of that code.

Basically it should be like:


  if window<=9 and window=last 1  character of variable (code1) then value=code1;

else if window<=9 and window=last 1 character of variable (code2) then value=code2

else if window>9  and window=last 2 character of variable (code11) then value=code11; *for 2 digit number;

...

So the value should be like:

1

.

4

.

6

.

.

6

.

Could you please help me with that?

Thank you so much.

HHC

data have;

input window code1 code2 code11;

datalines;

2 2 1 3

3 3 3 4

1 4 5 6

4 6 7 8

2 8 6 3

6 9 5 7

7 1 4 5

11 4 5 6

10 5 6 7

;run;

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Why not create an ARRAY and use window as the subscript?

data have;
   input window code1 code2 code11;
   datalines;
2 2 1 3
3 3 3 4
1 4 5 6
4 6 7 8
2 8 6 3
6 9 5 7
7 1 4 5
11 4 5 6
10 5 6 7
;;;;
   run;
data window;
   set have;
   keep window code1--code11 value;
   array code[11];
   value = code[window];
  
run;
proc print;
  
run;

Capture.PNG

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The more I think about it the less I like it.  What is code, how did it get onto the data?  Why does the precision change for variables > 9?  Why does code and window not match one-one?   Too many questions really. 

Well,

data want;

  set have;

  if window <= 9 and window=input(substr(strip(reverse(put(code1,best.))),1,1),best.) then value=code1;

  else if window <= 9 and window=input(substr(strip(reverse(put(code2,best.))),1,1),best.) then value=code1;

...

run;

data_null__
Jade | Level 19

Why not create an ARRAY and use window as the subscript?

data have;
   input window code1 code2 code11;
   datalines;
2 2 1 3
3 3 3 4
1 4 5 6
4 6 7 8
2 8 6 3
6 9 5 7
7 1 4 5
11 4 5 6
10 5 6 7
;;;;
   run;
data window;
   set have;
   keep window code1--code11 value;
   array code[11];
   value = code[window];
  
run;
proc print;
  
run;

Capture.PNG
hhchenfx
Barite | Level 11

Thank you, Data_Null,

It work perfectly.

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