I am trying to load a csv file contain a date-time variable. The variable looks like the following
datetime
2008-10-08T07:06:08.248635000Z
2008-10-08T07:06:09.613897000Z
2008-10-08T07:06:28.217422000Z
2008-10-08T07:07:53.461926000Z
2008-10-27T16:10:49.189132000Z
I tried format time18.3, but because there is a character T after the date and character Z after the time, so the importing is not successful. Could anyone teach me how to load this data please.
That's B8601DZw.d format; so you can use B8601DZ30. I believe.
data _null_;
dt_char='2008-10-08T07:06:08.248635000Z';
dt_num = input(dt_char,B8601DZ30.);
put dt_num= datetime.;
run;
Related
I am very new to SAS and would like to import a CSV file into SAS. I have tried using proc and also the tool import data, but fail to import the data.
i get the following errors:
ERROR: The name Page Name (prop31) is not a valid SAS name
when importing the data it doesnt accept the delimiter and imports everything as one column
any suggestions?
Thank you!
I am a SAS Developer. I currently have a script that will read from a table column that is in datetime format. In this script, it looks something like this:
data a; batch_dttm = '01Jan2011:00:00:00'dt; run;
proc sql; select batch_dttm format=16.0 into:batch_dttm from a; quit;
So when I assign it to macro variable, it is actually assigning the value of 2930485 into batch_dttm.
The problem is, when I want to resolve this &batch_dttm in another job at a later stage, i have to use this:
input(&batch_dttm,16.0)
to convert 2930485 into date.
I don't want to resolve in this way as this is the only Macro variable that has to be resolved with this input function. I want to assign 01Jan2011:00:00:00 (as text?) in PROC SQL INTO statement so that i dont have to use input conversion anymore.
I want to call &batch_dttm as datetime format in another script. I only want to resolve the datetime using "&BATCH_DTTM"dt instead of input(&batch_dttm,16.0). I believe there is a step to convert 01Jan2011:00:00:00 into text without changing it to 2930485. Is there anyway to do so?
How can I add 1 more step to make me resolve the macro in below script:
"&batch_dttm"dt
Why do you think you need to add the INPUT() function? You can just use the value of the macro variable to generate the number of seconds, 2930485 in your example, into your code.
SAS stores datetime values as a number of seconds, so these two expressions are the same:
where batch_dttm = 2930485 ;
where batch_dttm = '01JAN2011:00:00:00'dt ;
Which means you can just use code like this to use your original macro variable.
where batch_dttm = &batch_dttm ;
If you do need to have the human friendly text in the macro variable, perhaps to use in a title statement, then just change the format you use when creating the macro variable.
select batch_dttm format=datetime20. into :batch_dttm trimmed ...
...
title "Data as of &batch_dttm";
where batch_dttm = "&batch_dttm"dt ;
You can also use %sysfunc() to call PUTN() to change the existing number of seconds into that style if you want.
select batch_dttm format=32. into :batch_dttm trimmed ...
...
title "Data as of %sysfunc(putn(&batch_dttm,datetime20.))";
where batch_dttm = &batch_dttm ;
Like this?
proc sql;
select put(batch_dttm, datetime20.) into:batch_dttm from a;
quit;
%put &batch_dttm;
I've got proc import from xlsx file with column names in polish language.
My simple proc looks like this:
proc import datafile = '/directory/file_name.XLSX'
out = libname.tablename
dbms = xlsx
replace;
run;
I would like to add somewhere ENCODING="LATIN2" so the columns don't look like:
Is it possible? And how?
I could do it in second step by renaming all the columns with some predefined list. but I don't want to do it like this yet. Maybe there is a better solution.
You need to specify the encoding of the file you are reading/importing.
Per SAS support, this can be specified in the filename statement.
I've tested it with SAS UE and csv files and it worked pretty well:
filename temp '/folders/myfolders/Raw data/iso8859.csv' encoding="utf-8";
proc import datafile = temp
out = utf8
dbms = csv
replace;
run;
Your code should then look like:
filename temp '/directory/file_name.XLSX' encoding="LATIN2";
proc import datafile = temp
out = libname.tablename
dbms = xlsx
replace;
run;
There's a few things going on here:
You can't control the encoding of the XLSX format file; that's a binary file (sort of), and SAS doesn't treat it like a text file. You can do this for CSVs, as that's read in as a text file, but not XLSX.
If you're importing a file in another encoding into SAS, your session encoding also matters. You will have to run your SAS session in the right encoding to have everything look right. See the documentation for more details on how to change your SAS session encoding.
But, there's a third option here, if you just want to get rid of the extra characters: options validvarname=v7 (or even v6). I believe this will prevent SAS's import engine from using any character other than A-Z 0-9 and underscore. It won't necessarily look pretty, though, it'll replace all of the other characters with underscores I suspect.
I want to convert the string 20160101000000 into datetime format using expression. I have used below date function
TO_DATE(PERIOD_END_DATE),'MM/DD/YYYY HH24:MI:SS')
But my table file is not loading. My session and workflow gets succeed. My target and source is also flatfile.
I want to change the string 20160101000000 into MM/DD/YYYY HH24:MI:SS for loading data into my target table.
You need to give exact format that looks so that to_date function can understand that format and converts it into date.
TO_DATE(PERIOD_END_DATE,'YYYYMMDDHH24MISS')
So here your date looks like YYYYMMDDHH24MISS (20160101000000).
There is often confusion with the TO_DATE function... it is in fact for converting a string into a date and the function itself is to describe the pattern of the incoming date. Now if you want to convert a date field to a specified date format you must use TO_CHAR
So using SAS, I have a number of SAS monthend datasets named as follows:
mydata_201501
mydata_201602
mydata_201603
mydata_201604
mydata_201605
...
mydata_201612
Each has account information at particular monthend. I want to stack the datasets all into one dataset using colon rather than writing out the full set statement as follows:
data mynewdata;
set mydata_:;
run;
However there is no datestamp variable within the datasets so when I stack them I will lose the monthend information for each account. I want to know which line refers to which monthend for each account. Is there a way I can automatically create a variable that names the table the row come from. for example the long winded way would be this:
data mynewdata;
set mydata_201501 (in=a) mydata_201502 (in=b) mydata_201503 (in=c)...;
if a then tablename = 'mydata_201501';
if b then tablename = 'mydata_201502';
if c...
run;
but is there a quicker way using colon along these lines?
data mynewdata;
set mydata_:;
tablename = _tablelabel_;
run;
thanks
I always find clicking on comment links annoying, so hopefully here's the answer in your context. Use the INDSNAME= SET statement option to assign the dataset name to a variable:
data mynewdata;
set mydata_: indsname=_tablelabel_;
tablename = _tablelabel_;
run;
N.B. you can call _tablelabel_ whatever you want, and you may wish to change it so it doesn't look like a SAS generated variable name.
INDSNAME= only became a SAS SET statement option in version 9.2
Just to be clear, with my particular code, where the datasets were named mydata_yyyymm and I wanted a monthend variable with datestamp, I was able to produce this using the solution provided by mjsqu as follows (obs and keep statement provided if required):
data mynewdata;
set mydata_: (obs=100 keep=xxx xxx) indsname=_tablelabel_;
format monthend yymmdd10.;
monthend = input(scan(_tablelabel_,-1,'_'),yymmn6.);
run;