BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Multipla99
Quartz | Level 8

Does anyone have a nice solution on how to multiply every first position with 2 and every second position with 1?

 

Please see example below.

%macro Check;
data Check;
	set sashelp.Margarin (keep= HouseID);
	HouseIDtxt=put(HouseId,7.0);
	%do i= 1 %to 7;
	HousIDPosition&i=substr(HouseIDtxt,&i,1);
	%end;
run;
%mend Check;
%Check

Every time &i is odd I want HousIDPosition&i to be multiplied by 2 and every time &i is even I want it multiplied by 1. 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Use the Mod Function in %Sysfunc. See if you can use this as a template.

 

%macro m;
   %do i = 1 %to 10;
      %if %sysfunc(mod(&i, 2)) = 0 %then %put &i. is even!;
      %else %put &i. is odd!;
   %end;
%mend;

%m

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Use the Mod Function in %Sysfunc. See if you can use this as a template.

 

%macro m;
   %do i = 1 %to 10;
      %if %sysfunc(mod(&i, 2)) = 0 %then %put &i. is even!;
      %else %put &i. is odd!;
   %end;
%mend;

%m
Multipla99
Quartz | Level 8

Btw, here is my final code:

%macro Check;
data Check;
	set sashelp.Margarin (keep= HouseID);
	HouseIDtxt=put(HouseId,7.0);
	%do i= 1 %to 7;
	 %if %sysfunc(mod(&i, 2)) = 0 %then %do;
	  HousIDPositionMult&i= substr(HouseIDtxt,&i,1)*1;
	 %end;
	 %else %do;
	  HousIDPositionMult&i= substr(HouseIDtxt,&i,1)*2;
	 %end;
	%end;
run;
%mend Check;
%Check
FreelanceReinh
Jade | Level 19

Instead of a macro you could use an array in the data step:

data check(drop=i);
set sashelp.Margarin(keep=HouseID);
array HousIDPositionMult[7];
do i=1 to 7;
  HousIDPositionMult[i]=mod(int(HouseID/10**(7-i)),10)*(mod(i,2)+1);
end;
run;

This would also avoid the unwanted notes "Character values have been converted to numeric values ..." in the log as it multiplies numbers, not character strings.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 596 views
  • 3 likes
  • 3 in conversation