I need to read a cvs file that has value such as "1 female" (for the sex variable) and I would like to read this value into a numeric variable that will have the value of 1 for females
Thanks
Yoav
When reading from a delimited file you want to use list mode input. With list mode the width of the informat is ignored. So your choices are limited to reading it into a character variable or creating your own informat.
For the first option you could then convert the string to a number using the INPUT() function. You could also define the character variable to have a length a just 1 and the other characters will be ignored when the line is read since there will be no place the store them.
data test1;
infile 'myfile.csv' dsd trucover ;
input id gender :$1. ;
gender_code = input(gender,1.);
run;
proc format ;
infile gender (upcase)
'1 FEMALE'=1 '2 MALE'=2
;
run;
data test2;
inflie 'myfile.csv' dsd truncover ;
input id gender_code :gender. ;
run;
Thanks a lot Tom
The problem is that I have many values like that. For example income is coded "1 21k-25k" "2 26k-30K" etc. Is there a way to extract just the numerical part of the value?
Read the values as the strings they are and then create new numeric variables from them.
data step1;
infile 'myfile.csv' dsd truncover;
length id cvar1-cvar10 $80 ;
input id cvar1-cvar10;
run;
data step2;
set step1;
array old cvar1-cvar10;
array new nvar1-nvar10;
do index=1 to dim(old);
new[index]=input(scan(old[index],1,' '),32.);
end;
run;
Meanwhile I found a real easy solution. One way to to it is, for example when the value for female is "2 female" your state sexn=input(sex,1.) and if turns sex into a mumeric variable sexn.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.