Hi,
I'm a new programmer and I stuck in how to change length of a variable.
I read a raw data file with variable in there like "front".
So, I use input $quote7 to get rid of quotation and read the variable as a num.
Now, I need to get this char length to be 5 in my proc content procedure. What should I do?
(I also confuse that difference between informal and format. If input statement is a informat, Can attrib can be a format statement? Also, will input statement influence your data? I used to think that informal only influence the way you read data. Once you read and print out your data, you should get number without quotation, and your length in content table also should be 5 instead of 7. But it's not, Could someone tell me why?)
Big thanks!!
You use a FORMAT to convert values to text and an INFORMAT to convert text to values. A numeric format is used to display (aka format) a numeric value and a character format to display character values. Similarly a numeric informat creates numeric values and a character informat creates character values. You use an INFORMAT with the INPUT statement or the INPUT(), INPUTN() and INPUTC() functions. You use a FORMAT with the PUT statement or the PUT(), PUTN() and PUTC() functions.
Do not get confused and think that FORMAT or INFORMAT are intended to be used to DEFINE your variables. They are just for permanently attaching a format or an informat to your variable. It will only cause the data step compiler to guess how to define the variable if the first place you reference the variable is in the FORMAT (or INFORMAT) statement. To explicitly define the variable use the the LENGTH statement (or the LENGTH= option of the ATTRIB statement). A FORMAT or INFORMAT that is attached to your variable is just an attribute of the variable. Like the variable's LABEL.
You can avoid the confusion you are having by explicitly defining the variable before using it in other statements, like FORMAT, INFORMAT or INPUT.
I have never had to use the $QUOTE informat. If I am reading from a delimited file using the DSD option then SAS will automatically remove the quotes added around values. If the value is already in a variable then just use the DEQUOTE() function to remove the quotes around the value.
Note that most SAS variables do not need either an INFORMAT or a FORMAT attached to them. The purpose of a format is to give SAS special instructions on how to display the value. SAS already knows very well how to display both character variables and numeric variables. The same for INFORMATs. SAS knows very well how to read numeric and character variables. The main excepts are DATE, TIME and DATETIME values as the way humans like to see the values as text is totally different that how the values would normally be displayed as text.
data want ;
length id 8 front $10 ;
infile 'myfile.csv' dsd truncover ;
input id front;
run;
If you define the variable length prior to the input statement then that's what the SAS compiler will be using to create the variable in the PDV. See below for myvar1.
data test;
infile datalines truncover dlm=' ' dsd;
length myvar1 myvar2 $5;
input @1 myvar1 $quote7. @1 myvar2 $7.;
datalines;
"front"
front
;
proc contents data=test;
run;quit;
proc print data=test;
run;
Not sure that I understand what you're writing for DSD and removing quotes. Can you please demonstrate in above code sample how this would be working?
...oh, got it, the colon input modifier does the trick.
data test;
infile datalines truncover dlm=' ' dsd;
length myvar1 myvar2 $5;
input @1 myvar1 $quote7. @1 myvar2 :$7.;
datalines;
"front"
front
;
proc contents data=test;
run;quit;
proc print data=test;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.