BookmarkSubscribeRSS Feed
sc5
Obsidian | Level 7 sc5
Obsidian | Level 7

Hi,

 

I have 2 datasets as below: 'insects' and 'test1'.

Data test1 has thousands of rows with many columns including binary 1/0 ins_ant, ins_bee. ins_beetle.

I want to calculate new columns 'total_weight' and 'total_id' based on the values in Data insects (sum of the corresponding "weight"s and "id"s.)

 

I'm using a proc format to do this, my code is below. 

I used type = 'I' because my input is a string (insect name) and outputs are numbers, and that's the informat type to use to the documentation here - SAS Help Center: Results: PROC FORMAT

 

But my final output dataset test2 has null values for the total_weight and total_id columns. 

 

What am I doing wrong? 

 

Or please let me know if there's a simpler way to do this (without using proc format, but also without hardcoding anything like names of insects/weights/ids/number of insects etc.) 

 

Thank you!!

 

code:

 

 

 


data insects;
length insect $100.;
	insect='ant';
	weight = 10;
	id0 = 1;
	output;

	insect='beetle';
	weight = 20;
	id0 = 2;
	output;

	insect='bee';
	weight = 30;
	id0 = 3;
	output;
run;

data fmt;
	set insects;
	length start $100;
	start = upcase(strip(insect));

	retain type 'I';

	fmtname = 'weight';
	label = weight;
	output fmt;

	fmtname = 'id';
	label = id0;
	output fmt;

	keep start fmtname label type;
run;

proc format cntlin=fmt;
run;

data test1;
	
	patient = 4772;
	ins_ant = 1;
	ins_bee = 1;
	ins_beetle = 0;
	output;

	patient = 6792;
	ins_ant = 0;
	ins_bee = 1;
	ins_beetle = 1;
	output;
run;

data test2;
	set test1;
	total_weight = 0;
	total_id0 = 0;

	array ins_all {*} ins_: ; 
	
	do i_local = 1 to dim(ins_all);
		total_weight = total_weight + ( ins_all[i_local] * input(upcase(strip(scan(vname(ins_all[i_local]),2,'_'))),weight.) );
		total_id0 = total_id0 + ( ins_all[i_local] * input(upcase(strip(scan(vname(ins_all[i_local]),2,'_'))),id.) );
	end;

	drop i_local;
run;

 

 

 

 

2 REPLIES 2
Astounding
PROC Star
You may need an iterative solution: fix the earliest problem, then see what problems remain.
Since you report no errors, we won't try to fix any errors. Instead, sort the data set fmt by FMTNAME, before feeding it in to PROC FORMAT.
Last I checked, a new value for FMTNAME triggers replacement of that format, not the addition to previously defined values.
sc5
Obsidian | Level 7 sc5
Obsidian | Level 7
Sorting worked, thank you! 🙂

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 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
  • 2 replies
  • 107 views
  • 0 likes
  • 2 in conversation