data id_2;
input "Type d'élément"n $ variabl $ E D $ id $;
datalines;
Q 2 . . 2
Re 2 1 informe .
AAAA BBBB CCCC DDDD EEEE
Re 2 3 non .
Re 2 99 refus AZRDFV
aqe A 23 A2 .
S T K . 10
;
Good morning, I want to write a table whose data starts at line 4 and line 3 is the header line. Can someone give me a code.
THANKS,
Gick
Use FIRSTOBS=4 in the INFILE statement.
Here is some sample code to get you started:
/*Create a text file to play with*/
filename in temp;
data _null_;
file in;
input;
put _infile_;
datalines;
Q 2 . . 2
Re 2 1 informe .
AAAA BBBB CCCC DDDD EEEE
Re 2 3 non .
Re 2 99 refus 10
A 23 2 AZRDFVaqe .
A 23 2 A2 .
S 50 . X 10
;
/* Display contents of the text file */
title "Contents of the text file";
data _null_;
file print;
infile in;
input;
put _infile_;
run;
title;
/* Write the input statement using the names of the variables as seen above */
data id_2;
infile in firstobs=4 truncover;
input AAAA:$2. BBBB CCCC DDDD:$15. EEEE;
run;
title "Resulting data set";
proc print data=id_2;
run;
title;
Result:
Resulting data set |
Obs | AAAA | BBBB | CCCC | DDDD | EEEE |
---|---|---|---|---|---|
1 | Re | 2 | 3 | non | . |
2 | Re | 2 | 99 | refus | 10 |
3 | A | 23 | 2 | AZRDFVaqe | . |
4 | A | 23 | 2 | A2 | . |
5 | S | 50 | . | X | 10 |
It is not clear what you are asking. Perhaps because you use the word "table" without enough context to make it clear which of the many different meanings for that word applies.
Do you want to create a DATASET from reading in-line data, like your code?
If so then use the FIRSTOBS= option on a INFILE statement that has DATALINES (or it original name CARDS) as the fileref. Use FIRSTOBS=4 to skip ALL of the non-data lines. SInce you have an INFILE statement you can also add the TRUNCOVER option so SAS will not try to read multiply lines is the last values on the line are empty.
infile datalines firstobs=4 truncover;
Of if you just want to skip the third row you could use an IF statement that tests the automatic variable _N_. Add this block before the INPUT statement.
if _n_=3 then do;
input;
delete;
end;
Do you want to create a DATASET from reading a existing TEXT file that has this format? Do you want to use the values in the third row as the variable names? If you have a delimited file (which your file does not look like) you could use PROC IMPORT to read the data, but it cannot get the names from any place other than the first row. But this macro can : https://github.com/sasutils/macros/blob/master/csv2ds.sas
Otherwise you can read the data using generic names. Then then read just the third line to get the names and use that data to generate a RENAME statement to rename the generic variable names.
If you want to define all of the variables as character you can read the text file just once.
data want;
length v1-v5 $50 ;
input v1-v5;
datalines;
Q 2 . . 2
Re 2 1 informe .
AAAA BBBB CCCC DDDD EEEE
Re 2 3 non .
Re 2 99 refus AZRDFVaqe A 23 A2 .S T K . 10
;
proc transpose data=want(firstobs=3 obs=3) out=names;
var _all_;
run;
proc sql noprint;
select catx('=',name,nliteral(col1)) into :renames separated by ' '
from names
;
quit;
data want;
set want;
if _n_=3 then delete;
rename &renames;
run;
Put perhaps your question is how to write out a "table" from that data? Do you mean you want to write the variable names into the third row of the output? What should go into the first two rows? And is the output a TEXT file? Or perhaps some other type of file format, like WORD or RTF, so you that you can embed the resulting table into what every report it is you are creating?
Here is the table I'm looking to get.
Thanks for your help.
Gick
this is how my starting table looks
Thanks for your help.
Gick,
So it seems you already have a dataset (note the diction). How is the data contained in this dataset brought into SAS, what is the original source?
@Gick wrote:
this is how my starting table looks
Thanks for your help.
Gick,
You keep using that word. Since we cannot program from a picture or from a table in a report let's assume you are using "table" to mean a dataset. So you want to start with dataset in this picture and produce a dataset that would look like the other picture.
You just need use RENAME= and FIRSTOBS= dataset options.
To generate the old=new name pairs from the first dataset you can use PROC TRANSPOSE and PROC SQL to create a macro variable.
proc transpose data=HAVE (firstobs=3 obs=3) out=names;
var _all_;
run;
proc sql noprint;
select catx('=',nliteral(_name_),nliteral(col1))
into :renames separated by ' '
from names
;
quit;
Now you can use that macro variable in a step that skips over the first three observations of the original dataset.
data want;
set have(firstobs=4 rename=(&renames));
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.