BookmarkSubscribeRSS Feed
pablorodriguez1
Calcite | Level 5

 

I have a data set with ID in rows and months in columns, as the one shown below.

 

I want to create an auxiliary column that records the first value that is not zero of each line.

 

IDM1M2M3M4M5 Auxiliary column
100887 8
2777.. 7
300009 9
409998 9
511111 1
602211  

 

Currently l am using this code, but I haven't been able to get the results I am looking for. Any ideas?

data new_ops04;     
set new_ops03;

array MONTHS (24) M1-M24;

RETAIN AUXILIARY_COLUMN 0;
	do i=1 to 24;
	IF MONTHS(i) ne 0 and AUXILIARY_COLUMN = 0 THEN
               AUXILIARY_COLUMN = MONTHS(i);
	end;
		drop i;
run;

 

Thanks a lot!

1 REPLY 1
art297
Opal | Level 21
data new_ops04;     
  set new_ops03;

  array MONTHS (24) M1-M24;

  do _n_=1 to 24;
    IF MONTHS(_n_) gt 0 THEN do;
      AUXILIARY_COLUMN = MONTHS(_n_);
      leave;
    end;
  end;
run;
 

Art, CEO, AnalystFinder.com

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to connect to databases in SAS Viya

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.

Discussion stats
  • 1 reply
  • 2410 views
  • 2 likes
  • 2 in conversation