SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
twildone
Pyrite | Level 9

Hi...I am trying to fill in the Major variable with the first non-missing entry until the next non-missing entry (its possible for the same StudentUID to have more than one major) and I am getting an error message that the variable Major is both character and numeric. Major is character with length 50 and StudentUID is numeric. If I exclude the "not missing(Major or" from the until statement, it executes but I don't get the proper output. Can anyone see why I am getting the error message....Thanks.

 

 

data StudentDegree1;
 	length a1 $50;
 	do until(not missing(Major) or last.StudentUID);
  	set StudentDegree2;
  	by StudentUID Major notsorted;
		a1=Major;
 	end;
 	do until(not missing(Major) or last.StudentUID);
  	set StudentDegree2;
 	by StudentUID Major notsorted;
		Major=a1;
  	output;
 	end;
 drop a1;
run;

Log Output:

NOTE: There were 153022 observations read from the data set WORK.STUDENTDEGREE2.
NOTE: The data set WORK.STUDENTDEGREE2 has 153022 observations and 7 variables.
NOTE: PROCEDURE SORT used (Total process time):
      real time           0.32 seconds
      cpu time            0.51 seconds
      

319        
320        data StudentDegree1;
321         	length a1 $50;
322         	do until(not missing(Major) or last.StudentUID);
323          	set StudentDegree2;
ERROR: Variable Major has been defined as both character and numeric.
324          	by StudentUID Major notsorted;
325        		a1=Major;
326         	end;
327         	do until(not missing(Major) or last.StudentUID);
328          	set StudentDegree2;
ERROR: Variable Major has been defined as both character and numeric.
329         	by StudentUID Major notsorted;
330        		Major=a1;
331          	output;
332         	end;
333         drop a1;
334        run;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

While I can't check it right now, I'm guessing that the DO UNTIL loop defines MAJOR as numeric.  To get around that, add this right after the LENGTH statement:

 

if 5=4 then set StudentDegree2;

 

Even though it never executes (obviously, 5 does not equal 4), its presence is enough to define all the variables in StudentDegree2.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Can you provide some example data so we can test your code?

twildone
Pyrite | Level 9

Hi...I have included a dataset below....

 

 

data Have;
    length StudentUID 8 Major $ 50 TextTerm $ 12;
    format StudentUID BEST12.  Major $CHAR50. TextTerm $CHAR12.;
    informat StudentUID BEST12. Major $CHAR50. TextTerm $CHAR12.;
    infile datalines4 dlm='7F'x missover dsd;
    input StudentUID : BEST32. Major : $CHAR50. TextTerm : $CHAR12.;
datalines4;
104 00-01 Trm 1C
104Industrial Maintenance Technician00-01 Trm 2C
104Industrial Maintenance Technician00-01 Trm 3C
105Production Art00-01 Trm 1C
106Cisco Training00-01 Trm 3C
106 00-01 Trm 1C
106 00-01 Trm 2C
106Microcomputer Systems Technician00-01 Trm 2C
107Auto Mechanics00-01 Trm 2C
107 00-01 Trm 3C
108 00-01 Trm 2C
108Microcomputer Systems Technician00-01 Trm 3C
108Cisco Training01-02 Trm 2C
108 01-02 Trm 3C
109Microcomputer Systems Technician00-01 Trm 2C
109 01-02 Trm 1C
109 01-02 Trm 2C
109Cisco Training01-02 Trm 2C
;;;;
Astounding
PROC Star

While I can't check it right now, I'm guessing that the DO UNTIL loop defines MAJOR as numeric.  To get around that, add this right after the LENGTH statement:

 

if 5=4 then set StudentDegree2;

 

Even though it never executes (obviously, 5 does not equal 4), its presence is enough to define all the variables in StudentDegree2.

twildone
Pyrite | Level 9

Hi Astounding....I did try your suggestions and it worked....thanks a ton!!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 851 views
  • 0 likes
  • 3 in conversation