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

So I have to do the exact same thing over and over again for multiple dataset, so I figured a macro might be helpful. This is the code I am trying to run.

%macro lab (fich, num);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

%let &num=%eval(&num+1);

if CRF&num=" " then CRF&num="MISSING";

run;

%mend lab;

%lab(temp,55);

%lab(temp1,60);

So basically, I have variables (CRF55, CRF56, CRF57..and so on) that I want it to insert the word "MISSING" when the character value is blank.  And then the same thing again for another dataset starting with var CRF60 (so this case will be CRF60, CRF61, CRF62 and so on). I have to do this for 4 varaibles per dataset and all in numerical order (e.g 55,56,57,58 and then another dataset for 60,61,62,63...etc.)

Can someone suggests an easy way to do this without manually coding every dataset?

The only other solution I can think of is create multiple datasteps and do the %let statement to change the num inbetween each datasets. but I feel there is a more efficient way to code this.

%macro lab (fich, num);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

run;

%let &num=%eval(&num+1);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

run;

%mend lab;

Thank you!!!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your better off looking at arrays:

data want;

     set have;

     array crf{*} _character_;

     do i=1 to dim(crf);

          if crf{i}="" then crf{i}="Missing";

     end

run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your better off looking at arrays:

data want;

     set have;

     array crf{*} _character_;

     do i=1 to dim(crf);

          if crf{i}="" then crf{i}="Missing";

     end

run;

ballardw
Super User

To add to RW9 you can also list specific variables instead of all character variables.

array crf{*} crf55 crf56 crf57;

or a variable list

array crf{*} crf55 - crf59;

Tom
Super User Tom
Super User

Why not just use a format instead of modifying the data?

proc format ; value $miss ' '='Missing'; run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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