BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
draroda
Fluorite | Level 6

Hi All,

I need to keep entire column blank if observation#1 is missing or 0 . 

 

Please do needful.

 

data x;
infile datalines missover;
 input b c  d e f g  ;
 datalines;
 0 1 0 1 0   
 1 2 5 7 9 1 
 ;

 data xx;
  set x;
  array all(*) b c d e f g;
  do i = 1 to dim(all);
  if _n_=1 and all(i) in (0,.) then do;
   all(i)=.;
   if _n_ > 1 then all(i)=.;
  end;
  end;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do a transpose of the first observation, and use SQL to create the variable list for call missing:

data x;
infile datalines missover;
input b c  d e f g  ;
datalines;
0 1 0 1 0   
1 2 5 7 9 1 
;

proc transpose data=x (obs=1) out=trans (where=(col1=.));
var _numeric_;
run;

proc sql noprint;
select _name_ into :names separated by ',' from trans;
quit;

data want;
set x;
call missing(&names);
run;

View solution in original post

6 REPLIES 6
ballardw
Super User

Consider:


@draroda wrote:

Hi All,

I need to keep entire column blank if observation#1 is missing or 0 . 

 

Please do needful.

 

data x;
infile datalines missover;
 input b c  d e f g  ;
 datalines;
 0 1 0 1 0   
 1 2 5 7 9 1 
 ;

 data xx;
  set x;
  array all(*) b c d e f g;
  do i = 1 to dim(all);
  if _n_=1 and all(i) in (0,.) then do;
   all(i)=.;
   if _n_ > 1 then all(i)=.;
  end;
  end;
run;

One way is to have the first values from the set. The RETAIN creates a set of variables that the values will be kept from observation to observation. Then they are only populated for the first record.  Place them into an array for handy reference.

After satisfied that the logic works you could use : Drop f_: ; to remove all the temporary variables from your data. The colon creates a list of all variables whose names start with f_ .

ChrisNZ
Tourmaline | Level 20

Like this?

 data xx;
  set x;
  array all  [6] b -- g;
  array first[6] _temporary_;
  do i = 1 to dim(all);
    if _n_=1 then first[i] = all[i]; 
    if not first[i] then all[i]=.;
  end;
run;

 

s_lassen
Meteorite | Level 14

It is probably more efficient to run a data step first to determine the columns to be set missing, e.g.:

%let nullvars=; /* no variables found yet */
data _null_;
  set have;
  length nullvars $200;
  array vars b--g;
  do _N_=1 to dim(vars);
    if not vars(_N_) then
      call catx(',',nullvars,vname(vars(_N_));
    end;
  if lengthn(nullvars) then
    call symputx('nullvars',catx('call missing(',nullvars,')');
  stop;
run;

data want;
  set have;
  &nullvars;
run;
Kurt_Bremser
Super User

Do a transpose of the first observation, and use SQL to create the variable list for call missing:

data x;
infile datalines missover;
input b c  d e f g  ;
datalines;
0 1 0 1 0   
1 2 5 7 9 1 
;

proc transpose data=x (obs=1) out=trans (where=(col1=.));
var _numeric_;
run;

proc sql noprint;
select _name_ into :names separated by ',' from trans;
quit;

data want;
set x;
call missing(&names);
run;
s_lassen
Meteorite | Level 14
Basically the same solution as my suggestion. With one exception: if there are no missing or zero values in the first row, the statement "CALL MISSING();" will be executed, which I think will provoke an error.
Kurt_Bremser
Super User

Yep, we need to safeguard against that:

data x;
infile datalines missover;
input b c  d e f g  ;
datalines;
0 1 0 1 0 2 
1 2 5 7 9 1 
;

proc transpose data=x (obs=1) out=trans (where=(col1=.));
var _numeric_;
run;

%let names=;
proc sql noprint;
select _name_ into :names separated by ',' from trans;
quit;

%if &names. > %then %do;
data want;
set x;
call missing(&names);
run;
%end;

Thanks to SAS for making %if %then %do - %end available in open code.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 600 views
  • 4 likes
  • 5 in conversation