Hi I am trying to import a tab delimited file in SAS that looks like this,
Names Points
Sumit1 10
Sumit2 20
SUmit4 30
SUmit5 85
SUmit6 90
SUmit7 39
hfgö®q-±òSÀ®téîÓVU«‘îj'n5E•d÷Yb#­AK$®SŽ†ÿ-ÍKÕw¿óå0"¤h—t0Ld 89
SUmit8 48
SUmit9 70
SUmit10 20
SUmit11 90
The first row represents column names.
I am using the following code to import the file,
data names;
infile "C:xxxxxxxx\names.txt"
delimiter='09'x MISSOVER DSD lrecl=32767 firstobs=2;
informat names $150.;
informat Points best32.;
format names $150.;
format Points best12.;
input names $
Points;
run;
and the sas data set after import looks like the following:
Names Points
Sumit1 10
Sumit2 20
SUmit4 30
SUmit5 85
SUmit6 90
SUmit7 39
hfgö®q-±òSÀ®téîÓVU«‘îj'n5E•d÷Yb#­AK$®SŽ†ÿ-ÍKÕw¿óå0"¤h—t0Ld .
So basically all the rows are not getting imported in sas and it stops at row 7 because of the presence of some unusual characters
(I don't know what what this characters are called).
There are 1000 files like this that I need to import. So I am using a macro to import the files.
Can somebody please help me how can I import this type of files in SAS.
Try this code....
DATA names;
LENGTH Names $ 91 Points 8 ;
FORMAT Names $CHAR91. Points BEST2. ;
INFORMAT Names $CHAR91. Points BEST2. ;
INFILE 'C:xxxxxxxx\names.txt'
LRECL=32767 ENCODING="LATIN1" TERMSTR=CRLF DLM='7F'x MISSOVER DSD ;
INPUT Names : $CHAR91. Points : ?? BEST2. ;
RUN;
Related
I'm working with a sas7dbat file that was created erroneously and trying to fix it. When the file was created, the data was all input unto a single column instead of multiples, and I can't figure out how to manipulate it to do this. I thought I'd be able to use infile and make the dlm "|" with dsd to remove the quotations on the name column, but it seems that this problem is harder than it looks.
I basically want to turn that one column into the six it was supposed to be and delete the quotations from the names. Here's what it looks like in SAS:
SAS7dbat
And here's the datalines in case they're needed:
1 0017|2020-04-09|"Jason Nguyen"|122L|500.0|$404.82
2 0017|2020-04-09|"Jason Nguyen"|407XX|100.0|$201.95
3 0177|2020-04-05|"Glenda Johnson"|144L|100.0|$91.01
4 0177|2020-04-05|"Glenda Johnson"|188X|100.0|$70.76
5 0177|2020-04-05|"Glenda Johnson"|733|2.0|$101,230.00
6 0177|2020-04-05|"Glenda Johnson"|777|5.0|$106.29
7 1843|2020-04-03|"George Smith"|122|100.0|$60.64
8 1843|2020-04-03|"George Smith"|122L|10.0|$303.18
9 1843|2020-04-03|"George Smith"|144L|50.0|$91.01
10 1843|2020-04-03|"George Smith"|188S|3.0|$52,629.48
11 1843|2020-04-03|"George Smith"|855W|1.0|$92,210.41
12 1843|2020-04-03|"George Smith"|908X|1.0|$51,920.87
13 9888|2020-04-11|"Sharon Lu"|100W|1,000.0|$20.14
14 9888|2020-04-11|"Sharon Lu"|122|50.0|$60.64
(each line is one column inside SAS)
Go back and fix the import code would be my suggestion otherwise use the SCAN() function.
data want;
set have;
var1 = scan(variableName, 1, '|');
var2 = input(scan(variableName, 2, '|'), yymmdd.);
format var2 date9.;
var3 = dequote(scan(variableName, 3, '|'));
....
run;
Another option is to write the file as is back to a text file and then import it using the DLM='|' option.
Untested:
proc export data=have outfile='myfile.txt' dbms=dlm replace;
delimiter='';
run;
proc import out=want datafile='myfile.txt' dbms=dlm replace;
delimiter='|';
run;
Given that it's only 6 variables though you may as well write the data step for that code anyways.
I'm new to SAS. I'm trying to read a txt file where the same variables are listed in multiple columns.
The first variable is the date. The second one is time, and the last one is Blood Glucose. Thanks a lot for your kindness and help.
Sincerely
Wilson
The data can be read using a list input statement with the : (format modifier) and ## (line hold) features specified.
glucose-readings.txt (data file)
01jan16 14:46 89 03jan16 11:27 103 04jan16 09:40 99
05jan16 09:46 105 11jan16 10:58 108 13jan16 10:32 109
14jan16 10:49 90 18jan16 09:32 110 25jan16 10:37 100
Sample program
data want;
infile "c:\temp\glucose-readings.txt";
input
datepart :date9.
timepart :time5.
glucose
##;
datetime = dhms(datepart,0,0,timepart);
format
datepart date9.
timepart time5.
datetime datetime19.
glucose 3.
;
;
proc print; run;
From the documentation INPUT Statement: List
:
... For a numeric variable, this format modifier reads the value from the next non-blank column until the pointer reaches the next blank column or the end of the data line, whichever comes first.
...
##
holds an input record for the execution of the next INPUT statement across iterations of the DATA step. This line-hold specifier is called double trailing #.
...
Tip The double trailing # is useful when each input line contains values for several observations.
Be sure to read the documentation, that is were you will find detailed explanations and useful examples.
I am trying to create a SAS table for keeping descriptions and names of output tables which includes a formatted date inside. However the output includes date unformatted.
My code:
data tablenames;
infile datalines delimiter=',';
input description: $30. sastablename: $30.;
attrib datetoday format=yymmdd6.;
datetoday = date();
mergedtext=catx('_',sastablename,datetoday);
output;
datalines;
Table for Customers,TfC
Table for Sales,TfS
;
The code output gives TfC_20688 for mergedtext variable.
My desired output for mergedtext variable is TfC_160822.
You need to let CATX() know to use the formatted value. Try using the VVALUE() function if your variables are already formatted. Otherwise use the PUT() function to apply the format you want.
data tablenames;
infile datalines delimiter=',';
input description: $30. sastablename: $30.;
attrib datetoday format=yymmddn8.;
datetoday = date();
mergedtext1=catx('_',sastablename,vvalue(datetoday));
mergedtext2=catx('_',sastablename,put(datetoday,yymmddn8.));
datalines;
Table for Customers,TfC
Table for Sales,TfS
;
P.S. Don't use two digit years.
You can use the PUT() function to convert the SAS date in datetoday (the value of which is 20688) to the yymmdd format you want.
43 data tablenames;
44 infile datalines delimiter=',';
45 input description: $30. sastablename: $30.;
46 mergedtext=catx('_',sastablename,put(date(),yymmddn6.));
47 put mergedtext=;
48 output;
49 datalines;
mergedtext=TfC_160822
mergedtext=TfS_160822
NOTE: The data set WORK.TABLENAMES has 2 observations and 3 variables.
Hi I am trying to export a tab delimited file in SAS that looks like this,
Names Points
Sumit1 10
Sumit2 20
SUmit4 30
SUmit5 85
SUmit6 90
SUmit7 39
hfgö®q-±òSÀ®téîÓVU«‘îj'n5E•d÷Yb#­AK$®SŽ†ÿ-ÍKÕw¿óå0"¤h—t0Ld 89
SUmit8 48
SUmit9 70
SUmit10 20
SUmit11 90
I am using the following code to import the file,
data names;
infile "C:xxxxxxxx\names.txt"
delimiter='09'x MISSOVER DSD lrecl=32767 firstobs=2;
informat names $150.;
informat Points best32.;
format names $150.;
format Points best12.;
input names $
Points;
run;
and the sas data set after import looks like the following:
Names Points
Sumit1 10
Sumit2 20
SUmit4 30
SUmit5 85
SUmit6 90
SUmit7 39
hfgö®q-±òSÀ®téîÓVU«‘îj'n5E•d÷Yb#­AK$®SŽ†ÿ-ÍKÕw¿óå0"¤h—t0Ld .
So basically all the rows are not getting imported in sas and it stops because of the presence of some unusual characters
(I don't know what what this characters are called) in row 7.
There are 1000 files like this that I need to import. So I am using a macro to import the files.
Can somebody please help me how can I import this type of files in SAS.
Try this code.....Change the lengths accordingly...
DATA names;
LENGTH Names $ 91 Points 8 ;
FORMAT Names $CHAR91. Points BEST2. ;
INFORMAT Names $CHAR91. Points BEST2. ;
INFILE 'C:xxxxxxxx\names.txt'
LRECL=32767 ENCODING="LATIN1" TERMSTR=CRLF DLM='09'x MISSOVER DSD ;
INPUT Names : $CHAR91. Points : ?? BEST2. ;
RUN;
I am trying to make character informat from the range values given in a dataset.
Dataset : Grade
Start End Label Fmtname Type
0 20 A $grad I
21 40 B $grad I
41 60 C $grad I
61 80 D $grad I
81 100 E $grad I
And here is the code i wrote to create the informat
proc format cntlin = grade;
run;
And now the code to create a temp dataset using the new informat
data temp;
input grade : $grad. ## ;
datalines;
21 30 0 45 10
;
The output i wanted was a dataset Temp with values :
Grade
A
B
A
..
Whereas the dataset Temp has values :
Grade
21
30
0
...
SAS Log Entry :
1146 proc format cntlin = grade;
NOTE: Informat $GRAD has been output.
1147 run;
NOTE: PROCEDURE FORMAT used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
NOTE: There were 5 observations read from the data set WORK.GRADE.
1148
1149
1150 data temp;
1151 input grade : $grad. ## ;
1152
1153 datalines;
NOTE: SAS went to a new line when INPUT statement reached past the end of a
line.
NOTE: The data set WORK.TEMP has 5 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
I am not able to understand why informat is not working. Can anyone please
explain where i am making my mistake.
INFORMATS convert characters to (characters or numbers). So you can't use START/END the way you are doing so, since that only works with numbers.
See the following:
proc format;
invalue $grade
'0'-'20'="A"
'21'-'40'="B"
'41'-'60'="C"
'61'-'80'="D"
'81'-'100'="E";
quit;
proc format;
invalue $grade
'21'='A';
quit;
The latter works, the former gives you an error. So, you could write a dataset with all 101 values (each on a line with START), or just write a format and do it in a second step (read in as a number and then PUT to the format).