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

I have copied the code from Learning SAS by Example to a T. But I keep getting an error (5 errors actually) telling me:

ERROR: You are trying to use the character format $LIKERT with the numeric variable Ques5 in data set LEARN.SURVEY.
 
Here is the code:
DATA learn.Survey;
INFILE '/home/u6240xxxx/LEARN/survey.txt';
INPUT ID Gender $ Age Salary Ques1-Ques5;
RUN;
LIBNAME Learn '/home/u624xxxx/Z_LearnOutput';
PROC FORMAT;
value $Gender 'M' = 'Male'
'F' = 'Female'
' ' = 'Not entered'
other = 'Miscoded';
value Age low-29 = 'Less than 30'
30-50 = '30 to 50'
51-high = '51+';
value $Likert '1' = 'Str Disagree'
'2' = 'Disagree'
'3' = 'No Opinion'
'4' = 'Agree'
'5' = 'Str Agree';
RUN;
Title "Data Set SURVEY with Formatted Values";
PROC PRINT Data=Learn.survey;
id ID;
VAR Gender Age Salary Ques1-Ques5;
Format Gender $Gender.
Age Age.
Ques1-Ques5 $Likert.
Salary Dollar11.2;
RUN;
a) I have had to fix errors from the text prior to this problem in order to get codes to work (Mostly widths)
b) Any other option besides $LIKERT ?
c) THANK YOU!
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

In the first DATA step, the statement

 

	INPUT ID Gender $ Age Salary Ques1-Ques5;

implies that the variables Ques1 through Ques5 are numeric.  But the $likert format is applicable only to character variables.  Most likely the INPUT statement would need to be changed to read QUES1-QUES5 as character variables.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

In the first DATA step, the statement

 

	INPUT ID Gender $ Age Salary Ques1-Ques5;

implies that the variables Ques1 through Ques5 are numeric.  But the $likert format is applicable only to character variables.  Most likely the INPUT statement would need to be changed to read QUES1-QUES5 as character variables.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
KateBA715
Fluorite | Level 6

Thank you!

I used both responders' suggestions as the text file was not already in a library and needed the Data and Infile/INPUT and Obviously now I know - the length statements.

SO, my final solution was removing the single quotations from the values 1-5 in $LIKERT and I also included the $ after the Ques1-Ques5 in the INPUT statement.  I tried one suggestion then the other, but Both adjustments together worked perfectly. 

 

DATA Learn.survey;
INFILE '/home/u6240xxxx/LEARN/survey.txt';
length ID 8 Gender $8 Age Salary 8 Ques1-Ques5 $1 ;
INPUT ID Gender $ Age Salary Ques1-Ques5;
RUN;
LIBNAME Learn '/home/u6240xxxx/Z_LearnOutput';
PROC FORMAT;
value $Gender 'M' = 'Male'
'F' = 'Female'
' ' = 'Not entered'
other = 'Miscoded';
value Age low-29 = 'Less than 30'
30-50 = '30 to 50'
51-high = '51+';
value $Likert 1 = 'Str Disagree'
2 = 'Disagree'
3 = 'No Opinion'
4 = 'Agree'
5 = 'Str Agree';
RUN;
Title "Data Set SURVEY with Formatted Values";
PROC PRINT Data=Learn.survey;
id ID;
VAR Gender Age Salary Ques1-Ques5;
Format Gender $Gender.
Age Age.
Ques1-Ques5 $Likert.
Salary Dollar11.2;
RUN;

 

I had numeric data ques(1-5) that I needed to be the char data depicted in the code. He started w/ the PROC FORMAT so I was unsure on how to get the file read. 

Thank you both so MUCH!! Lesson Learned!! - KBA

Tom
Super User Tom
Super User

Removing the quotes from the format statement, without removing the $ from the name, will just confuse the HUMANS that need to read and understand your code.  SAS will know that you meant to create a CHARACTER format because the name starts with a $, so it will convert the NUMERIC range values into CHARACTER range values.  Save yourself some confusion by keeping the quotes if you want to define a CHARACTER format.

Tom
Super User Tom
Super User

You are trying to attach a CHARACTER format to NUMERIC variables.

So which do you want to fix?

Do you want to define the format as a NUMERIC format?  

value Likert
 1 = 'Str Disagree'
 2 = 'Disagree'
 3 = 'No Opinion'
 4 = 'Agree'
 5 = 'Str Agree'
;

Do you want to define the variables as CHARACTER variables?

Either define them as character with a LENGTH statement

DATA learn.Survey;
  INFILE '/home/u6240xxxx/LEARN/survey.txt';
  length ID 8 Gender $8 Age Salary 8 Ques1-Ques5 $1 ;
  INPUT ID Gender Age Salary Ques1-Ques5;
RUN;

or use a CHARACTER informat when reading them with the INPUT statement.

DATA learn.Survey;
  INFILE '/home/u6240xxxx/LEARN/survey.txt';
  INPUT ID Gender :$8. Age Salary (Ques1-Ques5) (:$1.);
RUN;

 

KateBA715
Fluorite | Level 6
Thank you! Like a charm!!
Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 5 replies
  • 1604 views
  • 0 likes
  • 3 in conversation