BookmarkSubscribeRSS Feed
sandrube
Fluorite | Level 6

Hello friends,

I have the following data set. I'm trying to create a variable 'nccs', which is first number between 11-45 among the variables DXCCS1-DXCCS5. That way, 'nccs' would be similar to variable 'Result'.

 

data data;
input ID DCCS1 DXCCS2 DXCCS3 DXCCS4 DXCCS5 Result;
datalines;
1 67 73 43 1 34 43
2 2 87 67 62 92 .
3 9 32 . 5 2 32
4 34 6 90 93 88 34
5 5 53 84 68 4 .
6 55 76 67 56 29 .
7 12 82 . . 11 12
8 10 49 76 67 99 .
9 78 24 56 93 11 24
10 3 1 2 82 99 .
run;

 

I used the following code:

 

data data_1;
set data;
array DXCCS DXCCS1-DXCCS5;
do h=1 to dim(DXCCS);
if DXCCS(h) ne . then do;
if nccs_CCS = . and (DXCCS(h)>11 and DXCCS(h)<45) then nccs = DXCCS(h);
end;
end;
run;

 

Can someone help me fix it.

 

Thank you and have a wonderful weekend!

 

Rube

 

 

2 REPLIES 2
Kurt_Bremser
Super User

Maxim 2: Read the Log.

 88         data data_1;
 89         set data;
 90         array DXCCS DXCCS1-DXCCS5;
 91         do h=1 to dim(DXCCS);
 92         if DXCCS(h) ne . then do;
 93         if nccs_CCS = . and (DXCCS(h)>11 and DXCCS(h)<45) then nccs = DXCCS(h);
 94         end;
 95         end;
 96         run;
 
 NOTE: Variable nccs_CCS is uninitialized.

The NOTE tells you that you try to use a variable that

  • does not exist in the incoming dataset
  • is never set to a value in the step

I guess that nccs_CCS is a typo and you wanted to use nccs.

FreelanceReinh
Jade | Level 19

Hello @sandrube,

 

You can use a DO-UNTIL loop:

/* Create sample data */

data have;
input ID DXCCS1-DXCCS5 Result;
datalines;
1 67 73 43 1 34 43
2 2 87 67 62 92 .
3 9 32 . 5 2 32
4 34 6 90 93 88 34
5 5 53 84 68 4 .
6 55 76 67 56 29 29
7 12 82 . . 11 12
8 10 49 76 67 99 .
9 78 24 56 93 11 24
10 3 1 2 82 99 .
;

/* Find first array value in ]11, 45[ */

data want(drop=h);
set have;
array DXCCS[*] DXCCS:;
do h=1 to dim(DXCCS) until(11<DXCCS(h)<45);
end;
if h<=dim(DXCCS) then nccs=DXCCS(h);
run;

Note that I've corrected your missing Result for ID 6 and the typo in "DCCS1".

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 1024 views
  • 0 likes
  • 3 in conversation