BookmarkSubscribeRSS Feed
kuridisanjeev
Quartz | Level 8

Hi all..

Iam having bellow code.

proc sort data=test;

by 'emp id'n 'emp target'n;

run;

data test1;

set test;

by 'emp id'n 'emp target'n;

if first.'emp id'n ne 1 then 'emp taget'n=0;

run;

if i run the code i am not getting the correct output.

i mean to say first. last. variables are not creating...

Some one saying it is because of variables...

please clarify me...

8 REPLIES 8
manojinpec
Obsidian | Level 7

First and last variables are hidden automatic variables. they will not be visible.If you want to view them assgin it to some difference variable.

proc sort data=test;

by 'emp id'n 'emp target'n;

run;

data test1;

set test;

by 'emp id'n 'emp target'n;

a=first.emp_id;

b=last.emp_id;

c=first.emp_target;

d-last.emp_target;

if first.'emp id'n ne 1 then 'emp taget'n=0;

run;

Ksharp
Super User

first. last. variables have already created by SAS.

But because they are temporary variables, they will not appear at PDV ,but at somewhere at memory.

And they will not output dataset defaultly.

Ksharp

kuridisanjeev
Quartz | Level 8

Sorry to all.

A small typo error happened in my above post.

I am not getting correct output.

there i am using

if first.'emp id'n ne 1 then 'emp taget'n=0  condition.

this condition was not working because the variable names that i mentioned as 'emp id'n and 'emp taregt'n.

if I change variable names to emp_id and emp_target, the condition was working....

So  what's wrong with variable names?????

manojinpec
Obsidian | Level 7

'n is used for what purpose?

kuridisanjeev
Quartz | Level 8

If you want to define  variable name with "space", generally we use n.

for example if you  want to create a variable with name "customer name",SAS won't accept this name directly because there is a space between customer and name..

For this you need to specify like this 'customer name'n.

Let me know if any clarification needed.

Thank you.

Sanjeev.

manojinpec
Obsidian | Level 7

Is it applicable for SAS 8.2 . I am using sas 8.2 and i am not able to assign this name.

kuridisanjeev
Quartz | Level 8

I don't think it works in 8.2.

this is the  new option which I found in SAS recently .

it is working in 9.2

Tom
Super User Tom
Super User

You need to set validvarname=any.  But you are probably better off NOT using it.  You can use the variable label instead to store the description of the variable.  Most procs will list the label along with the variable name.  You can use the LABEL option for PROC PRINT to have the column headers be the label instead of the name.

1     data x;
2      'This name'n=100;
ERROR: The name This name is not a valid SAS name.
3    run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.X may be incomplete.  When this step was stopped there were 0 observations and 0 variables.

4    options validvarname=any;
WARNING: Only the Base product and the SAS/STAT product have been tested for use with VALIDVARNAME=ANY. Other use of this option is considered
         experimental and may cause undetected errors.
5     data x;
6      'This name'n=100;
7    run;

NOTE: The data set WORK.X has 1 observations and 1 variables.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1404 views
  • 0 likes
  • 4 in conversation