SAS 4GL - multiple informats - sas

Im learning 4Gl and I have little problem with informats:
I have file:
Imie;Nazwisko;Wiek;indeks;PESEL;Kierunek;Rok;Urodziny;Srednia;Frekwencja
Tomasz;Szan;23;114132;9134765445;Informatyka;5;5.32;99%;14.03.91
Karolina;Herl;21;134294;93543245;;3;4.57;92%;29.09.93
Damian;Kwak;24;189994;1234567890;Informatyka;5;3.50;80%;24.09.90
Ebenezer;Scrooge;AA;882741;78899609;Automatyka;4;3.72;34%;30.02.88
And 4GL code:
DATA projekt.project1;
length PESEL $ 11;
length nazwisko $ 15;
length kierunek $ 15;
INFILE 'c:\lasa_do_sasa\studenty.txt' DLM=';' MISSOVER DSD FIRSTOBS=2;
INPUT imie $ nazwisko $ wiek $ nr_indeksu PESEL $ kierunek $ rok srednia_ocen frekwencja PERCENT3. urodziny ddmmyy8. ;
RUN;
The problem is that: if I have xx%;date SAS won't read date. Im getting error:
Invalid data for urodziny
anyone could help me? I tihnk Im doind something obvious...

The trick here is to use the : format modifier to stop SAS trying to read beyond the next delimiter after the % sign. You can also set the lengths of your other variables on the input statement this way:
data want;
infile cards4 dsd dlm = ';' firstobs = 2;
input imie $ nazwisko :$15. wiek $ indeks $ PESEL :$11. kierunek :$15. rok urodziny srednia :PERCENT3. frekwencja ddmmyy8. ;
format frekwencja ddmmyy8.;
cards4;
Imie;Nazwisko;Wiek;indeks;PESEL;Kierunek;Rok;Urodziny;Srednia;Frekwencja
Tomasz;Szan;23;114132;9134765445;Informatyka;5;5.32;99%;14.03.91
Karolina;Herl;21;134294;93543245;;3;4.57;92%;29.09.93
Damian;Kwak;24;189994;1234567890;Informatyka;5;3.50;80%;24.09.90
Ebenezer;Scrooge;AA;882741;78899609;Automatyka;4;3.72;34%;30.02.88
;;;;
run;

Related

IF THEN statement in SAS

I'm not sure why this code doesn't work in SAS. May someboday help, please?
DATA WORK.POLLUTION;
INPUT State $ County $ City $ Month $ Year $ O3MAX $ Category;
IF O3MAX < 0.054 THEN Category = "Good";
ELSE IF O3MAX < 0.070 THEN Category = "Moderate";
ELSE IF O3MAX < 0.085 THEN Category = "UnhealthySensitive";
ELSE IF O3MAX < 0.105 THEN Category = "Unhealthy";
ELSE IF O3MAX < 0.200 THEN Category = "VeryUnhealthy";
ELSE Category = "Dangerous";
RUN;
PROC PRINT DATA = WORK.POLLUTION;
TITLE= "O3";
RUN;
The input statement tells SAS how to read from a text file, but it does not specify where to look for the tekst. That you do with an infile statement, like in
DATA WORK.POLLUTION;
INFILE "C:\myFolder\myInput.txt";
INPUT State $ County $ City $ Month $ Year $ O3MAX $ Category;
...;
run;
Optionally you can give the information on the file upfront by giving it a name and referring it:
filename MY_TEXT "C:\myFolder\myInput.txt";
DATA WORK.POLLUTION;
INFILE MY_TEXT;
INPUT State $ County $ City $ Month $ Year $ O3MAX $ Category;
...;
run;
A special filename is datalines, which refers to inline data, between a datalines statement and a semicolon
DATA WORK.POLLUTION;
INFILE datalines;
INPUT State $ County $ City $ Month $ Year $ O3MAX $ Category;
...;
datalines;
<your data comes here>
;
run;
If you don't specify an INFILE statement infile datalines; is implicitly used, but as you don't give a datalines; statement, there is no input. I bet your log tells something about zero lines read.
By the way, why do you specify Category in the input statement? I suppose it is only in your output.
There is a lot more to say about the infile statement. You have for instance options to handle lines with not all data filled in, like trunkover, which you should read about.

SAS: dlm and dsd?

I am confused about what DSD actually does in terms of "moving the pointer" and reading in data. To better explain, look at the following code:
data one;
infile cards dlm=',' TRUNCOVER ; /*using dlm','*/
input cust_id date ddmmyy10. A $ B $ C $;
cards;
1,10/01/2015,5000,dr
;
run;
data two;
infile cards dsd TRUNCOVER ;
input cust_id date ddmmyy10. A $ B $ C $;
cards;
1,10/01/2015,5000,dr
;
run;
The dataset one contains values for A and B of 5000 and dr but the dataset two contains values of A as missing whereas B and C are 5000 and dr. I don't get why the dsd sets A to missing.
Thanks!
Your problem is not DLM or DSD it is "DATE DDMMYY10." that is inFORMATTED input which is not compatible with delimited input in any form DSD or NO.
You need INFORMAT statement or : informat modified.
date :DDMMYY10.

Why these many observations are created in the following program?

data test;
infile cards dsd dlm=', .';
input stmt : $ ##;
cards;
T
;run;
/*-----------------------------------------------*/
data test;
infile cards dsd dlm=', .';
input stmt : $ ##;
cards;
Th
;run;
/*-----------------------------------------------*/
data test;
infile cards dsd dlm=', .';
input stmt : $ ##;
cards;
This is SAS.
;run;
When first program is run, 80 observations are created
When second program is run, 79 observations are created
When third program is run, 72 observations are created
I know these program has worst programming style. Wrong options are set for wrong technique. DSD option is set, double trailing operator ## (line holder), Colon modifier (:) are used and more than 1 delimeter is used which is worst SAS programming ever.
Aside from this I want to know why so many observations are created, why 80? 79? how program is executed? I think DSD option & 2 delimeters have major impact. Can anyone explain?
The reason you get more records than you expect is because CARDS are fixed length records. The reason you get a difference number of records is because there is a different number of null fields left after reading the non-null field(s). You can see this by adding the COL option to the INFILE statement to show you where the column pointer is after reading each field. Col=3, 4 , 13
data test;
infile cards dsd dlm=', .' col=c;
input stmt : $ ##;
col=c;
cards;
T
;run;
proc print data=test(obs=5);
/*-----------------------------------------------*/
data test;
infile cards dsd dlm=', .' col=c;
input stmt : $ ##;
col=c;
cards;
Th
;run;
proc print data=test(obs=5);
/*-----------------------------------------------*/
data test;
infile cards dsd dlm=', .' col=c;
input stmt : $ ##;
col=c;
cards;
This is SAS.
;run;
proc print data=test(obs=5);
run;

How to reference file correctly in filevar option in SAS?

Here is a simple code to read two files
filename one "C:\Users\Owner\Desktop\SAS\nbExce\ch1\1-12 one.txt" ;
filename two "C:\Users\Owner\Desktop\SAS\nbExce\ch1\1-12 two.txt" ;
data test ;
input extfile $ ;
infile dummy filevar=extfile end=last ;
do until (last) ;
input name $ score ;
output ;
end ;
datalines ;
one
two
;
run ;
proc print ;
run ;
Why do I get this error ? How can I improve my file references ?
You cannot refer to file assigned using a filename statement in the filevar. Use the full path to the files.
data test;
infile datalines dsd;
length extfile $128;
input extfile $;
infile dummy filevar=extfile end=last;
do until (last);
input name $ score;
output;
end;
datalines;
"C:\Users\Owner\Desktop\SAS\nbExce\ch1\1-12 one.txt"
"C:\Users\Owner\Desktop\SAS\nbExce\ch1\1-12 two.txt"
;
run;

SAS programming : Convert Exponential value to numeric value in SAS

How to convert exponential(for eg. 3.22254e2, 3.24456545e-3) values to numeric format(322.254,0.00324456545) in SAS. I am getting the source as varchar from a file and need to store the same in oracle as number format.
I need to read from a file(csv) so When i tried to do the same i get the result(b) as null.
My code:
data work.exp_num ;
infile 'exp_number.csv'
lrecl = 256
delimiter = '~'
dsd
missover
firstobs = 2;
;
attrib a length = $300
format = $32.
informat = $32.;
input a ;
run;
data test;
set work.exp_num;
b=input(a,32.16);
run;
Kindly help.
Thanks in advance.
You can use the standard informat w.d .
Here you got an example:
data test;
a='3.24456545e-3'; output;
a='3.22254e2'; output;
run;
data erg;
set test;
b=input(a,32.16);
run;