Thank you for your response! I further explored your code and found that the FILE and FILENAME statements indeed produces a temporary file in the SAS Temporary Files dataset but is not executed immediately after running the DATA step. The %include statement causes the immediate execution. This process, from my persepctive, is like first producing a jack-in-a-box with the DATA step and then use the %include the statement to open it.
@Tom wrote:
When you say "imported into SAS from a spreadsheet" do you mean you used PROC IMPORT to read from an XLSX file? If so then the only reason MEMNAME would be numeric in that dataset would be if none of the cells where character strings. In which case the issue is in the data step. When compiling a data step SAS will define the TYPE of a variable as soon as it has to. Usually when it first SEES the variable. If SAS cannot tell from the context that the variable should be character it will define it as numeric.
Is it possible your metadata does not have a variable named MEMNAME?
Yes, I did create the dataset named metadata with Excel. Strange though, the error was stubborn despite a check in the imported metadata dataset that proved that MEMNAME was indeed character instead of numeric. I therefore had to assign a character format to MEMNAME in the DATA step code like this:
filename code temp;
data _null_;
set metadata;
file code ;
* Write data statement and INFILE statement ;
put 'data ' memname:$60. '(label=' memlabel :$quote. ');'
' infile "cars.csv" dsd truncover firstobs=2;'/*Replace the address of the CSV with a real one in the case of importation*/
;
* Write LENGTH statement ;
put 'length ' @;
do p=1 to nobs;
set metadata point=p nobs=nobs;
put name @;
if type='string' then put '$' @;
put length @;
end;
put ';' ;
* Write informat statement ;
put 'informat ' @;
do p=1 to nobs;
set metadata point=p ;
if informat ne ' ' then put name informat @;
end;
put ';' ;
* Write format statement ;
put 'format ' @;
do p=1 to nobs;
set metadata point=p ;
if format ne ' ' then put name format @;
end;
put ';' ;
* Write label statement ;
put 'label ' @;
do p=1 to nobs;
set metadata point=p ;
if label ne ' ' then put name '=' label :$quote. @;
end;
put ';' ;
* Write input statement ;
p=1;
set metadata point=p;
put 'input ' name '-- ' @ ;
p=nobs;
set metadata point=p;
put name ';' ;
* Write run statement ;
put 'run;' ;
stop;
run;
%include code / source2;
In addition, I found the LABEL statement lacking an equal sign and added one in the code. In addition, I found SAS keep running the code endlessly without a STOP statement at the end. The addition leads to a successful importation.
Still, I would still like to ask a few questions on formats. It can be seen from the code that when using the PUT statement to execute commands, a colon (:) has to be added between the name of format and the name of variable. However, if ordinary DATA steps, this is not required (e.g., Make $13 instead of Make: $13.). I wonder if my summarization is correct and if there are other instances where a colon is needed. Much appreciation is given if you could elaborate on the reason why such a difference exists.
In addition, a dot has to be appended to the quote. format while those seem to be not necessarily needed for character formats with predefined length (e.g., $60 and $60. are both OK) as well as numeric formats. Could you please give a reason for this? In addition, are the formats with and without a following dot at the end of their names really the same or they are different yet this case cannot reveal this difference?
... View more