Hi,
I get a code and want to turn it into macro.
data Apple_price(drop=Age);
length key $25 Apple_price_c $10 Factor_c $10;
set in.Apple_price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
Apple_price_c = 'Apple_price';
Factor_c = 'Factor';
run;
Since I've pear and peach data as well. But my code seems wrong. Could anybody can give some suggestions?
%macro fruit(covlist);
%if &covlist. ^= none %then %do;
%let y = 1;
%let cov = %scan(&covlist., &y., ' ');
%do %until(&cov. eq %nrstr( ));
data &cov._price(drop=Age);
length key $25 &cov._price_c $10 Factor_c $10;
set in.Apple_price;
key = %compress('Case'||ID||'_'||Group||'_'||%put(Unit,z2.),,'kadp');
&cov._price_c = %compress("&cov."||'_price',,'kadp');
Factor_c = 'Factor';
run;
%let y = %eval(&y. + 1);
%let cov = %scan(&covlist., &y., ' ');
%end;
%end;
%mend fruit;
%fruit(covlist = apple pear peach)
Many Thanks!
Take your code:
data Apple_price(drop=Age); length key $25 Apple_price_c $10 Factor_c $10; set in.Apple_price; key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp'); Apple_price_c = 'Apple_price'; Factor_c = 'Factor'; run;
Use the CHANGE command to convert APPLE to &COV. If you are using the SAS editor just type this on the command line:
change 'Apple' '&cov.' i all
data &cov._price(drop=Age);
length key $25 &cov._price_c $10 Factor_c $10;
set in.&cov._price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
&cov._price_c = '&cov._price';
Factor_c = 'Factor';
run;
Then make sure that your macro variable reference is not inside of string quoted used single quotes instead of double quotes.
data &cov._price(drop=Age);
length key $25 &cov._price_c $10 Factor_c $10;
set in.&cov._price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
&cov._price_c = "&cov._price";
Factor_c = 'Factor';
run;
Why are you calling the %COMPRESS() macro? Do you have a definition for that macro?
678 %put %compress(); WARNING: Apparent invocation of macro COMPRESS not resolved.
Why did you insert a %PUT statement into the middle of one of the lines of SAS code you are trying to get the macro to create?
680 %put(Unit,z2.),,'kadp'); (Unit,z2.),,'kadp')
Only change the parts of the program that are actually changing.
But my code seems wrong.
Explain further. What is wrong? We don't know what you mean.
Also, run this line of code first
options mprint;
and then execute your macro again, and then show us the LOG. Click on the {i} icon and paste your code as text into the window that appears. Do not skip this step. This is an important step to maintain the formatting of the LOG and make it much more readable.
set in.Apple_price;
Your hard coded your input data set?
I suspect you need to be using your macro variable there as well.
@jordenlam wrote:
Hi,
I get a code and want to turn it into macro.
data Apple_price(drop=Age);
length key $25 Apple_price_c $10 Factor_c $10;
set in.Apple_price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
Apple_price_c = 'Apple_price';
Factor_c = 'Factor';
run;Since I've pear and peach data as well. But my code seems wrong. Could anybody can give some suggestions?
%macro fruit(covlist); %if &covlist. ^= none %then %do; %let y = 1; %let cov = %scan(&covlist., &y., ' '); %do %until(&cov. eq %nrstr( )); data &cov._price(drop=Age); length key $25 &cov._price_c $10 Factor_c $10; set in.Apple_price; key = %compress('Case'||ID||'_'||Group||'_'||%put(Unit,z2.),,'kadp'); &cov._price_c = %compress("&cov."||'_price',,'kadp'); Factor_c = 'Factor'; run; %let y = %eval(&y. + 1); %let cov = %scan(&covlist., &y., ' '); %end; %end; %mend fruit; %fruit(covlist = apple pear peach)
Many Thanks!
Take your code:
data Apple_price(drop=Age); length key $25 Apple_price_c $10 Factor_c $10; set in.Apple_price; key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp'); Apple_price_c = 'Apple_price'; Factor_c = 'Factor'; run;
Use the CHANGE command to convert APPLE to &COV. If you are using the SAS editor just type this on the command line:
change 'Apple' '&cov.' i all
data &cov._price(drop=Age);
length key $25 &cov._price_c $10 Factor_c $10;
set in.&cov._price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
&cov._price_c = '&cov._price';
Factor_c = 'Factor';
run;
Then make sure that your macro variable reference is not inside of string quoted used single quotes instead of double quotes.
data &cov._price(drop=Age);
length key $25 &cov._price_c $10 Factor_c $10;
set in.&cov._price;
key = compress('Case'||ID||'_'||Group||'_'||put(Unit,z2.),,'kadp');
&cov._price_c = "&cov._price";
Factor_c = 'Factor';
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.