Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SAS Data Sets Manipulations

Posted on 2010-08-18
8
Medium Priority
?
1,095 Views
Last Modified: 2013-11-16
I am trying to use external MACRO throughout my program to get a number value from "var1" to inside of one of my file names "file_&var1.DAT":

Note:
1.  "var1" is not in the dataset "data1"
2.  "var1" may be a number from 01 to 99

%let var1=%SYSGET (var5);
%MACRO START;

data _null_;
file 'dirname:file_&var1.DAT';
 set lib.data1
  put @1 var2 $char2.
        @3 var3 $char2.
        @5 var4 $char3.
        @8 var5 $char2.
        @10 var6 $char1.;
run;
     
%MEND START;
%START;

Please let me know what am I doing wrong here?
I am getting the following WARNING: The argument to macro function %SYSGET is not defined as a system variable.
0
Comment
Question by:labradorchik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 9

Accepted Solution

by:
bradanelson earned 1000 total points
ID: 33469814
Where is var5 coming from in the following statement?
%let var1=%SYSGET (var5);


Once you have the GLOBAL VARIABLE var1 set, here is how to use it in the filename.  Notice the double quotes and the extra period I placed after the GLOBAL VARIABLE name.

%MACRO START;

data _null_;
file "dirname:file_&var1..DAT";
 set lib.data1
  put @1 var2 $char2.
        @3 var3 $char2.
        @5 var4 $char3.
        @8 var5 $char2.
        @10 var6 $char1.;
run;
     
%MEND START;
%START;
0
 

Author Comment

by:labradorchik
ID: 33469946
"var5" number value should come from the "var5" from the data1 data set. Am I doing something wrong here?
0
 
LVL 7

Expert Comment

by:d507201
ID: 33470042
%sysget is used to access environment variables like sasroot.  I don't think you want to have it here unless you're using the -sysparms statement when SAS session initializes.  

You're not trying to use the SYMGET function to retrieve a macro variable, are you?  If so, you don't neet it.  If var5 is already a macro variable then you need to use this syntax:
  %let var1=&var5;

The macro processor resolves &var5 and assigns the text value to var1.  The symget function is used in a DATA step and resolves at execution time, not compile time, so it's very handy.

If you work with macros, you really should get Art Carpenter's book "Carpenters Complete Guide to the SAS Macro Language."  All SAS professionals should have a copy, I think..
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 11

Assisted Solution

by:theartfuldazzler
theartfuldazzler earned 1000 total points
ID: 33472833
Hi

Try the CALL SYMPUT function.

DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

%PUT *** &Var1 ***;

This takes the value of Var5 in the dataset Data1 (from the last record) and assigns it to the macro variable Var1
     
0
 

Author Comment

by:labradorchik
ID: 33473916
Thanks guys, but none of the above code is currently working for me...
I tried   %let var1=&var5; instead of %let var1=%SYSGET (var5);

than I tried
DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

%PUT *** &Var1 ***;

In both cases I am getting the following message: WARNING: Apparent symbolic reference VAR5 not resolved.
I thought I referenced "var5" in my data step's PUT statement?!? Am I doing it incorrectly?

I can see my output file "file_.DAT" now, but this file should appear in my directory as "file_50.DAT".
Note: if "var5"=50 for example
0
 

Author Closing Comment

by:labradorchik
ID: 33474169
I am sorry, below code is actually working fine, - I can see now file with the "var5" value embedded in it.
Thanks!!!

DATA _NULL_;
   set Data1;
      CALL SYMPUT('Var1',Var5);
RUN;

Also,
"dirname:file_&var1..DAT";   is the correct format!
Thanks!
0
 

Author Comment

by:labradorchik
ID: 33479001
Only one thing is not working:

My output file "file_&var1.DAT" only contains the last record' value from "var1", - all other values are missed or not recorded.

Here is what I am trying to get:
For every different value of "var1" in the "data" SAS Dataset I am trying to output file "file_&var1.DAT.
So, if SAS dataset "data1" has total of 5 records and values for "var1" are for example 09, 09, 67, 78, 98, than I should have total of 4 "file_&var1.DAT" files as my output:
file_09.DAT
file_67.DAT
file_78.DAT
file_98.DAT

Right now for my output file I am only getting  file_98.DAT, which is my last record' value, which in this case is 98.
How come all other values for "var1" are not recorded and output files are not made?
Thanks!
0
 
LVL 9

Expert Comment

by:bradanelson
ID: 33479050
You will have to create a loop to process the mulitple output datasets needed.  1st, you read the file in to find out how many variations there are of var5 and then loop through a process to create a new dataset for each distinct variation.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question