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