I have copied the code from Learning SAS by Example to a T. But I keep getting an error (5 errors actually) telling me:
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;
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.
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.
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
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.