dataset using datalines infile with two dates variables - sas

My datalines has 2 variables, date 1 and date 2 with corresponding format ddmmyy10. and mmddyy10.
data date;
input date1 ddmmyy10. date2 mmddyy10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
11/01/2015 01/11/2015
12/01/2015 01/12/2015
13/01/2015 01/13/2015
;
run;
I tried to add the code but still not work
infile datalines delimiter=' ';

The : (colon) format modifier enables you to use list input but also to specify an informat after a variable name, whether character or numeric. SAS reads until it encounters a blank column, the defined length of the variable (character only), or the end of the data line, whichever comes first.
Format Modifier
data date;
input date1: ddmmyy10. date2: mmddyy10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
11/01/2015 01/11/2015
12/01/2015 01/12/2015
13/01/2015 01/13/2015
;
run;

The reason why the code is not working is because you are using List input to read non-standard data without colon input modifier or informat statement.
For non-standard data(commas, dollar, date etc. -> Reading Raw data -> Kinds of Data ) or standard data of length greater than 8 byte using List input technique you would need to use either INFORMAT statement or colon modifier with INPUT statement.
1) Assign informat for the input variables using INFORMAT statement or ATTRIB statement
data date;
informat date1 ddmmyy10. date2 mmddyy10.;
input date1 date2;
format date1-date2 yymmdd10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
;
run;
2) Use Colon (:) input modifier
data date;
input date1: ddmmyy10. date2: mmddyy10.;
format date1-date2 yymmdd10.;
datalines;
09/01/2015 01/09/2015
10/01/2015 01/10/2015
;
run;

Related

Reading data with unequal widths in SAS

I have this dataset and i would like to read it. I still encounter some error. May I know how would i be able to solve them?
data ThreeDates;
infile datalines;
input Date1 ddmmmyy10. Date2 ddmmyy10. Date ddmmyy9.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;
You need to add a : modifier to read non-standard data. It specifies an informat that the INPUT statement uses to read the variable value. The purpose is to read data values that need the additional instructions that informats can provide but that are not aligned in columns. If data was aligned in columns, you would use #n (line pointer controls).
You should also use the date9. informat for the Date column.
data ThreeDates;
infile datalines;
input Date1 :mmddyy10. Date2 :mmddyy10. Date :date9.;
format date: mmddyy10.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;
More information about lists in INPUT Statement.
PS: Instead of the : modifier, you could also use the INFORMAT statement before the INPUT statement:
data ThreeDates;
infile datalines;
informat date1-date2 mmddyy10. date date9.;
input Date1 Date2 Date;
format date: mmddyy10.;
datalines;
01/03/1950 01/03/1960 03Jan1970
05/15/2000 05/15/2002 15May2003
10/10/1998 11/12/2000 25Dec2005
;
run;

SAS programming, read from a file and separate into different column

I have a excel-file where I want to split words into different columns in SAS.
In the file it looks like this in the same column, I want to split it and get rid of quotation marks :
ID;"City";"Year"
1;"New york";NULL
2;"stockton";"18"
This is what I tried to do:
data work.project ;
infile "&path\users.csv" delimiter=';' missover dsd;
input ID: $30.
City: $200.
Year: $5. ;
run;
proc print data=work.project;
run;
My output:
Obs ID City Year
1 ,,,"ID ""City"" ""Year
2 ,,,"1 ""new york"" NULL"
3 ,,,"2 ""stockton"" ""18"
4 ,,,"3 ""moscow "" NULL"
Rather than the colon and formats in the INPUT statement use an INFORMAT statement.
data work.project;
infile datalines4 delimiter=';' truncover dsd;
informat id $30. city $200. year $4.;
input ID City Year;
datalines4;
1;"New York";NULL
2;"Stockton";"18"
;;;;
run;
proc print data=project;
run;

SAS: Using macro to import multiple text files

I am using SAS to import hundreds of csv files.
There are 300 city data-sets with the following naming convention (Shanghai001-Shanghai100, London001-London100, Newyork001-Newyork100).
My current import code is
data shanghai001;
infile 'H:\shanghai001.csv'
delimiter = ',' DSD lrecl=32767 firstobs=2;
informat Date_L_ DATE11.;
informat Time_L_ time18.3;
informat Type $10.;
format Date_L_ DATE11.;
format Time_L_ time18.3;
format Type $10.;
input
Date_L_
Time_L_
Type $
;
run;
This code works, but I just want to know how to use macro to import these 300 data sets?
Any smart guy can tell me ?
You could use a macro to do this, but you do not need to. It would probably be much easier to read all of the CSV files into a single data set. You could add variables like FNAME and VERSION to tell which observations came from which source file.
data all_data;
length fname $100 path $200 version 8 ;
length Date_L_ Time_L_ 8 Type $10.;
informat Date_L_ date11. Time_L_ time18.3 ;
format version z3. Date_L_ date11. Time_L_ time12.3 ;
do fname='shanghai','london','newyork';
do version=1 to 100 ;
path = catx('\','H:',cats(fname,put(version,z3.),'.csv'));
if not fileexist(path) then do;
put 'ERROR: File not found. ' path=:$quote.;
continue;
end;
infile csvfile filevar=path dsd truncover end=eof;
if not eof then input ;
do while (not eof);
input Date_L_ Time_L_ Type ;
output;
end;
end;
end;
run;

Character date 31.03.2001 to numerical, SAS

I have a variable that was entered as 31.01.2002 for all entries, and is a character. I would like to put it in numerical form with date9. .
I have tried the below:
date=input(oldway, 10.);
date=input(oldway, date9.);
put date=ddmmyy10.;
date=input(compress(oldway,'.'),10.);
date = INPUT(compress(oldway),date9.);
format date date9.;
run;
I have also tried combinations of the above and to no avail.
Any ideas for forward motion?
Kind Regards!!
You can't input your date using the date9. informat as your string variable isn't in that format. You can use ddmmyy10., though, and that also takes care of the . characters.
data have;
input old $10.;
cards;
31.01.2014
28.02.2014
01.01.2015
;
run;
data want;
set have;
new = input(old, ddmmyy10.);
format new date9.;
run;
try this:
data _null_;
date ="31.01.2014";
date=compress(date,".");
new_date=input(date,ddmmyy8.);
format new_date date9.;
put new_date;
run;

using dsd and comma informat at the same time

I have the below raw data
1,,35,000
2,100,45,000
and need the below in a dataset
1 . 35000
2 100 45000
this would require both dsd option and using comma. informat.
How to carry this out?
DSD has nothing to do with this - DSD involves input like
1,,"35,000"
2,100,"45,000"
If that is what you have, then you can use the : operator to read it in with the comma informat.
data test;
infile datalines dlm=',' dsd;
input id
num
dollar :comma8.;
datalines;
1,,"35,000"
2,100,"45,000"
;;;;
run;
If you do not have the quotes around the field, then you will need to parse this somehow. One solution is below, which will work as long as the field with commas is the final field.
data test;
infile datalines dlm=',' dsd;
input #;
if countc(_infile_,',') =3 then do;
_commapos = findc(_infile_,',',-1*length(_infile_));
_infile_ = substr(_infile_,1,_commapos-1)||substr(_infile_,_commapos+1);
end;
input id
num
dollar ;
put _all_;
datalines;
1,,35,000
2,100,45,000
;;;;
run;
If the field your potential is in is in a consistent field, but NOT the first one, you can modify the above solution to correct it. If it's in potentially more than one field, you have a much more difficult problem to solve.