BookmarkSubscribeRSS Feed
umashankersaini
Quartz | Level 8

Hi All,

I am reading a dataset and creating macro variable with respect to observation (specific) value. Created macro variable would be used in another code. Currently i am writing in following way (for example) :

Data DSN;

input X $17. @18 Y $15. ;

Cards;

Value of PROJECT Most_valuable

Trans of Money   Already done

Cost to company  Disclosed

Company to Cost  Paid

Run;

options symbolgen mprint;

Data _NULL_;

Set DSN;

If trim(upcase(X))='VALUE OF PROJECT' Then call symput ("PROJECT",(Y));

Run;

Data _NULL_ ;

Set DSN;

if trim(upcase(X))='TRANS OF MONEY' then call symput ("Money",(Y));

Run;

.

.

and so on.. I have approx 50 observation to create macro variable. Please help me to write that in PROC SQL with specified condition.

Log should show something for this :

%Put &Project &Money;

SYMBOLGEN:  Macro variable PROJECT resolves to Most_valuable

SYMBOLGEN:  Macro variable MONEY resolves to Already done.

Please help.

Thanks in advance

Umae

3 REPLIES 3
Haikuo
Onyx | Level 15

May I ask why? the following will NOT be the same if the condition is not unique. It will take the first one while your data step takes the last one that meets your condition. It can be tweaked, but before I know your whole picture, it may not worth the extra mileage.

proc sql;

select y into :project from dsn where trim(upcase(X))='VALUE OF PROJECT' ;

select y into :money from dsn where trim(upcase(X))='TRANS OF MONEY' ;

quit;

Reeza
Super User

That doesn't make a huge amount of sense to me.

If you know a variable maps to something you want to create a macro variable for make a master list somewhere.

Untested and can probably be made more efficient but hopefully points you in the right direction. The alternative is a table similar to my proc format, join via sql and then a similar Call symputx statement.

proc format;

invalue $ mv_fmt

'TRANS OF MONEY' = 'MONEY'

"VALUE OF PROJECT' = 'PROJECT';

RUN;

data _null_;

set have ;

if trim(upcase(x)) in ('TRANS OF MONEY', 'VALUE OF PROJECT') THEN DO;

     call symputx(input(trim(upcase(x)), y);

end;

run;

Tom
Super User Tom
Super User

Why not make a format or informat to translate the value of X to the macro variable name?

Here is example using an INFORMAT.

proc format ;

  invalue $mvarname (upcase just)

'VALUE OF PROJECT'='PROJECT'

'TRANS OF MONEY'='MONEY'

'COST TO COMPANY'='COMPANY'

'COMPANY TO COST'='COST'

  ;

run;

data DSN;

  input X $17. @18 Y $15. @1 mvarname $mvarname17. ;

cards;

Value of PROJECT Most_valuable

Trans of Money   Already done

Cost to company  Disclosed

Company to Cost  Paid

run;

proc print; run;

data _null_;

  set dsn ;

  call symputx(mvarname,y);

run;

%put _user_;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 845 views
  • 2 likes
  • 4 in conversation