Help using Base SAS procedures

Missing observations and reorder variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Missing observations and reorder variables

Hello,

I have some problems with some datasets I obtained from Datastream ( I attach an example).

1) Because Datastream uses NA for missing values, I tried to convert all the variables to numeric (as these were assigned as character ones). However, many observations were assigned a (.) even though there was a value (see for example a4).

2) Secondly, I cannot get the order of the variables correctly. Instead of a1 to a313, I get amixed order.

Anyone can help on this?

Here is the code I used:

proc contents data=have out=vars(keep=name type) noprint;

data vars;                                               

set vars;                                                

if type=2 and name ne 'id';                              

newname=trim(left(name))||"_n";

options symbolgen;

proc sql noprint;

select trim(left(name)), trim(left(newname)),            

       trim(left(newname))||'='||trim(left(name))        

into :c_list separated by ' ', :n_list separated by ' ', 

     :renam_list separated by ' '                        

from vars;                                               

quit;    

data test2;                                              

set have;                                                

array ch(*) $ &c_list;                                   

array nu(*) &n_list;                                     

do i = 1 to dim(ch);                                     

  nu(i)=input(ch(i),8.);                                 

end;                                                     

drop i &c_list;                                          

rename &renam_list;                                                                                     

run;

data new2;set test2; retain a1-a313;run;

Attachment

Accepted Solutions
Solution
‎06-22-2013 01:16 PM
Respected Advisor
Posts: 3,777

Re: Missing observations and reorder variables

I would just skip all that and use a data step to read the file.  Note the NUMX informat.

data have313;
   infile '~/Have.txt' dsd dlm='09'x firstobs=2 lrecl=2048 stopover;
  
input date:ddmmyy. (a1-a313)(:numx.??);
   format date ddmmyy10.;
  
run;

View solution in original post


All Replies
Solution
‎06-22-2013 01:16 PM
Respected Advisor
Posts: 3,777

Re: Missing observations and reorder variables

I would just skip all that and use a data step to read the file.  Note the NUMX informat.

data have313;
   infile '~/Have.txt' dsd dlm='09'x firstobs=2 lrecl=2048 stopover;
  
input date:ddmmyy. (a1-a313)(:numx.??);
   format date ddmmyy10.;
  
run;
Frequent Contributor
Posts: 75

Re: Missing observations and reorder variables

Many thanks data_null_!

It works fine!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 239 views
  • 0 likes
  • 2 in conversation