BookmarkSubscribeRSS Feed
Astounding
PROC Star

@CP2,

 

As long as you are forcing yourself to learn macro language, here are some tools to consider.  I'll give you the short tour.

 

You are starting with these macro variables:

 

%let a = gender, age  ; 

%let b= age height weight gender ;

%let b2= "unk1" "unk2" "unk3"  "unk4";  

 

From that, you need to add calculations to the DATA step:

 

data want;

set have;

if age = '' then age="unk1" ;

if gender = "" then gender="unk4" ;

run;

 

The programming was complex, but ultimately you found a solution.

 

If you had started with a different set of macro variables, the task would be straightforward.  Starting with:

 

%let a = gender age  ; 

%let age = "unk1";

%let height = "unk2";

%let weight = "unk3";

%let gender = "unk4";

 

From that starting point, the solution is easier:

 

data want;

set have;

%do i=1 %to %sysfunc(countw(&a));

   %let varname = &scan(&a, &i);

   if &varname = ' ' then &varname = &&&varname;

%end;

run;

 

If the programming would be that easy, it must be worthwhile to investigate ... how do you take your original set of macro variables and create the second set from them?

 

In most cases, getting commas out of macro variables can only help.  The simplest way to do that is in a DATA step:

 

data _null_;

call symputx('a', translate("&a", ' ', ','));

run;

 

The other transformations can be handled by macro language:

 

%do i=1 %to %sysfunc(countw(&b));

   %let &scan(&b, &i) = %scan(&b2, &i);

%end;

 

Done, and ready to use in the relatively simple solution.

 

That's the quick tour (omitting %local statements).

 

 

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
  • 15 replies
  • 2808 views
  • 4 likes
  • 6 in conversation